Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support conditionally setting arguments to a command #130

Closed
huonw opened this issue Aug 25, 2023 · 5 comments
Closed

Support conditionally setting arguments to a command #130

huonw opened this issue Aug 25, 2023 · 5 comments
Labels
enhancement New feature or request

Comments

@huonw
Copy link
Collaborator

huonw commented Aug 25, 2023

In pantsbuild/scie-pants#249, we encounter a circumstance where a command has an arg that needs to be conditionally set: a binding computes an env var that is sometimes set to --python-repos-find-links=... and sometime empty. If the env var is empty, it'd be better for the command to not receive an argument: invoking pants "" ... causes issues with pants specifically, and in general passing a spurious empty string is likely to cause problems for any program.

It'd be handy to be able to (optionally) make an argument conditional, so that these sort of "maybe set an argument" uses cases are easier to handle.

AFAICT, this isn't currently possible with scie-jump: there's a reified argument for every argument in the manifest, and string like "{scie.bindings.configure:PANTS_SHA_FIND_LINKS}" just becomes an empty string.

jump/jump/src/context.rs

Lines 270 to 274 in 1403e1f

for arg in &cmd.args {
let (reified_arg, needs_manifest) = self.reify_string(&env, arg)?;
needs_lift_manifest |= needs_manifest;
args.push(reified_arg.into());
}

jump/jump/src/context.rs

Lines 576 to 581 in 1403e1f

let value = binding_env
.get(&parsed_env.name)
.map(String::to_owned)
.or(parsed_env.default)
.unwrap_or_default();
reified.push_str(&value)

Suppose there's a manifest like the following, we'd like to be able to annotate the ARGUMENT arg to disappear completely in some cases (particularly when ARGUMENT is empty, but a more general mechanism would be okay too):

...
"commands": {
  "": {
    "exe": ".../executable"
    "args": ["{scie.bindings.some_binding:ARGUMENT}"]
  }
}
...

In shell terms:

ARGUMENT=""

.../executable "$ARGUMENT" # current behaviour: passing a "" argument

.../executable $ARGUMENT # requested behaviour: no argument at all

Workaround that we'll likely use in pantsbuild/scie-pants#250: instead of setting the env var to nothing, set it to a "no-op" argument, so that we're never passing an empty arg to the executable.

(This may apply to arguments in bindings in addition to commands?)

@huonw huonw added the enhancement New feature or request label Aug 25, 2023
huonw added a commit to pantsbuild/scie-pants that referenced this issue Aug 25, 2023
Currently, the `scie-pants` launcher sometimes needs to pass an
additional argument to the underlying pants invocation, to be able to
support `PANTS_SHA` and its custom wheel location. Effectively turning
an invocation like `pants goal --arg` into `pants
--pants-repos-find-links=+["..."] goal --arg` when appropriate:
https://github.com/pantsbuild/scie-pants/blob/0a204c5ac816ad24d74b55ecf7424397a07b3c2a/package/scie-pants.toml#L57-L60

However, when not using `PANTS_SHA`, the pants command becomes `pants ""
goal --arg`, and the spurious empty string results in some commands
failing, e.g. `pants "" tailor --check` thinks `--check` is a global
argument, and this is ambiguous.

To fix this issue, this PR does two things:

1. avoid passing an empty string, via a no-op workaround: always set the
`PANTS_SHA_FIND_LINKS` variable (to a no-op like
`--python-repos-find-links=-[]`), so that scie-pants never passes an
empty variable to the pants command, since that makes commands like
`pants tailor --check ::` explode:
2. deprecates `PANTS_SHA`, so that the hack above doesn't feel so bad:
we're on the way to removing the need for `PANTS_SHA_FIND_LINKS`
entirely, thus sidestepping the limitation and the need for the
workaround (2)

This could also be fixed via a-scie/jump#130.

Fixes #249
@jsirois
Copy link
Collaborator

jsirois commented Aug 25, 2023

@huonw as I noted here: pantsbuild/scie-pants#249 (comment), scie-jump doesn't support dynamic commands, the lift manifest is static save for interpolation. None the less, commands can be selected dynamically by using recursive dispatch. This is what scie-pants already does to handle the debug case. I'd really like to keep scie-jump simple as per the general thrust outlined here: https://github.com/a-scie/jump/blob/main/CONTRIBUTING.md#guiding-principles

Does the recursive dynamic dispatch of commands that scie-pants already does suffice for you @huonw?

@huonw
Copy link
Collaborator Author

huonw commented Aug 25, 2023

Yep, happy to call that sufficient for this. I assume you aware of the combinatorial explosion (if one has 2 args that might need to disappear independently, I think one will need 4 commmands for the different combinations of present vs not; 3 args => 8 commands, etc?), but, as I said, happy to call this one answered.

@huonw huonw closed this as not planned Won't fix, can't repro, duplicate, stale Aug 25, 2023
@jsirois
Copy link
Collaborator

jsirois commented Aug 25, 2023

Yeah, I'm aware Pants would need to go from 2 to 4 commands the way it's currently doing things. My initial thought was to not do things this way (not have the existing configuration / install handle PEX installs), but to have Pants release an unpacked scie (PEX + manifest) where each release of Pants could have arbitrarily complex bespoke to the version complexity (see: #10).

All that said, isn't scie-pants / Pants engaged in hoisting and petards here? IIUC one can say "--python-repos-find-links=[]" to indicate to Pants a base set of 0 find links repos (or via env var), or "--python-repos-find-links=foo to append one, etc? I.E. I think it's the case that scie-pants could have solved this totally in house in a clean way.

@jsirois
Copy link
Collaborator

jsirois commented Aug 25, 2023

I noted what seems to me to be the root bug here: pantsbuild/scie-pants#226 (comment)

@huonw
Copy link
Collaborator Author

huonw commented Aug 25, 2023

Yeah, in this case, there's an "obvious" no-op value, so that's the fix we went for in pantsbuild/scie-pants#250

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants