Skip to content

Always use bundled getopt_long implementation#40104

Closed
fingolfin wants to merge 3 commits intoJuliaLang:masterfrom
fingolfin:mh/uniform-getop
Closed

Always use bundled getopt_long implementation#40104
fingolfin wants to merge 3 commits intoJuliaLang:masterfrom
fingolfin:mh/uniform-getop

Conversation

@fingolfin
Copy link
Copy Markdown
Member

... not just when building with MSVC. This ensures that command line
arguments are processed uniformly on all platforms. It also gives us
full control over how it behaves.

Resolves #29669, that is, typos like julia --projec=t. (note how = and t are swapped) won't be silently accepted anymore.

The code is a bit hacky; I could rewrite it to get rid of the #define hack. On the other hand, it keeps the diff minimal and would make it very easy to revert this in the future again.

@fingolfin
Copy link
Copy Markdown
Member Author

Test fails with this error:

  Expression: readchomperrors(`$exename --startup-file=no --inline`) == (false, "", "ERROR: option `--inline` is missing an argument")
   Evaluated: (true, "", "") == (false, "", "ERROR: option `--inline` is missing an argument")

I guess that means the "new" getopt code enabled here really wants --inline=yes instead of just --inline. Of course that's an unacceptable regression, and should be fixed. (On the upside, that means people using a Julia on MSVC always suffered from this discrepancy, and now that we know, it can be fixed).

Given that this getopt code is from musl, but is fairly old (~2014?) the first thing I'd try is to see if their newer version works better, and whether it could be used. However, it seems to have been changed substantially, so I am not sure how viable this is.

Or one could just improve the code that we have already, it shouldn't be too hard for just this one usecase.

However, before I investigate either option, it'd be good to know whether this PR has any chance of landing at all -- if this is not something the Julia core devs want out of some principle, there is no point in me trying to fix this (and possible other) issue(s).

@goretkin
Copy link
Copy Markdown
Contributor

I think a change like this needs at least a deprecation cycle, where any args that fail due to this PR , would succeed but with a deprecation warning. Something that would show up in e.g. CI logs and give people a chance to transition.

@vtjnash
Copy link
Copy Markdown
Member

vtjnash commented Apr 8, 2021

@vtjnash
Copy link
Copy Markdown
Member

vtjnash commented May 29, 2021

Would you be willing to update to the new musl code, so we do get consistent behavior on all platforms?

@vtjnash
Copy link
Copy Markdown
Member

vtjnash commented Oct 14, 2021

It should just require downloading those patches, and using patch getopt.c < $file.patch to update it

@fingolfin
Copy link
Copy Markdown
Member Author

I will take another look

@fingolfin
Copy link
Copy Markdown
Member Author

So I've looked into this a bit again, and got it working with the most recent getopt_long in musl. However, the code needs at least some patching, plus some hackery to avoid clashes between its symbol and those define in the systems' version of getopt(_long). How to handle that?

  • forget about upstream and just maintain our modified version (and then possibly refactor it; e.g. get rid of the tons of global variables)
    • this makes in particular sense if there are further changes we expect to need to make
  • carry a patch version, but in addition also the patch and/or the original sources and/or a precise description where the base / vendor version came from

Any preferences?

@vtjnash
Copy link
Copy Markdown
Member

vtjnash commented Oct 18, 2021

If we're unhappy enough, we can also use the command line parser in LLVM instead from libSupport, but I think it just hasn't been a major issue enough to be worthwhile making significant changes.

... not just when building with MSVC. This ensures that command line
arguments are processed uniformly on all platforms. It also gives us
full control over how it behaves.

Resolves JuliaLang#29669
@fingolfin
Copy link
Copy Markdown
Member Author

Actually when I just tried to rebase and update the PR, I noticed that PR #42586 was merged which actually removes the use of src/getopt.c (but the code was left in).

So I've now made PR #42703 which actually removes src/getopt.c, as it is currently unused. That doesn't render this PR here useless, though; it is still about resolving inconsistencies in how Julia parses its command line options between platforms.

If this is still a goal, then I can also revive this PR. E.g. since dropping support for "abbreviated" options like --proj=foo (instead of --project=foo) would technically be breaking, we can investigate switching to another implementation, such as the FreeBSD one as suggested by @vtjnash

@vtjnash
Copy link
Copy Markdown
Member

vtjnash commented Oct 19, 2021

It seems possibly still useful to vendor our own copy for consistency.

@tecosaur
Copy link
Copy Markdown
Member

tecosaur commented Oct 3, 2024

From a reading of this PR, it seems like it may still be relevant, despite the linked PRs merged?

@fingolfin fingolfin closed this Nov 7, 2025
@fingolfin fingolfin deleted the mh/uniform-getop branch November 7, 2025 08:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Stricter command line flag checking

4 participants