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

feat: run scripts without -- #4290

Merged
merged 2 commits into from Jan 30, 2022
Merged

Conversation

gluxon
Copy link
Member

@gluxon gluxon commented Jan 30, 2022

Fixes #3778.

Behavior Change

There's a lot of scenarios mentioned in the original issue that make all solutions ambiguous/non-ideal. While still not perfect, I think the behavior below will cause the least amount of confusion:

  • Anything after the <command> portion of pnpm run <command> is passed directly to <command> itself.
  • Any command line flags before the <command> portion is treated as an option to pnpm run.
  • For backwards compatibility, the first occurrence of -- anywhere after <command> is swallowed.

For example:

  • This allows pnpm run --help to still show the help text for pnpm run.
  • While pnpm run changeset --help will show the help text of whatever command drives the changeset script.

Notes

@@ -207,13 +207,59 @@ test('any unknown command is treated as a script', async () => {
fallbackCommand: 'run',
getCommandLongName: () => null,
universalOptionsTypes: { filter: [String, Array] },
}, ['foo', '--recursive'])
}, ['--recursive', 'foo'])
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to rearrange the --recursive flag here to fix the test after these changes. This was expected from the behavior change described in the pull request description.

@@ -134,6 +134,11 @@ export async function handler (
) {
let dir: string
const [scriptName, ...passedThruArgs] = params
// For backward compatibility
const firstDoubleDash = passedThruArgs.findIndex((arg) => arg === '--')
Copy link
Member Author

@gluxon gluxon Jan 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pattern matched this from the same change in pnpm exec: #3492

// For backward compatibility
if (params[0] === '--') {
params.shift()
}

pnpm v6 allowed -- anywhere in the args portion of pnpm run <command> [..args], so this extra .findIndex logic tries to maintain full backwards compatible behavior for that.

Although perhaps we should only swallow the first -- like pnpm exec does. I could see the behavior in the scenario below being a bit surprising. This is how pnpm behaves before and after this pull request.

❯ pnpm run --silent echo a -- b
a b

You might expect:

❯ pnpm run --silent echo a -- b
a -- b

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although the behavior above didn't change in this PR, I do worry this will come back to bite someone that thinks -- can be dropped entirely after this change. Just a heads up @zkochan.

Happy to make any additional followup changes before the pnpm v7 release if we want to.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is fine if we don't swallow -- at all. This is a breaking change in v7. So it won't be a surprise to anyone.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Cool. I'll open a pull request reverting this block later tonight when I'm near my computer.

@gluxon gluxon marked this pull request as ready for review January 30, 2022 03:18
@gluxon gluxon requested a review from zkochan as a code owner January 30, 2022 03:18
Comment on lines +2 to +4
"@pnpm/parse-cli-args": major
"@pnpm/plugin-commands-script-runners": major
"pnpm": major
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these all be major?

Comment on lines +107 to +109
const indexOfRunScriptName = 1 +
(recursiveCommandUsed ? 1 : 0) +
(fallbackCommandUsed && opts.fallbackCommand === 'run' ? -1 : 0)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic here might be a bit of a hacky way to do things. We could also:

  • run nopt again to find the script name to use as the escape arg. That would result in 3 nopt calls here.
  • add additional behavior in pnpm's nopt fork to dynamically determine whether something is an escape arg. I'm imagining a closure that gets passed the partial CLI parsing up to that point.

@@ -107,7 +107,7 @@ For options that may be used with `-r`, see "pnpm help recursive"',
FILTERING,
],
url: docsUrl('run'),
usages: ['pnpm run <command> [-- <args>...]'],
usages: ['pnpm run <command> [--] [<args>...]'],
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we just drop the [--] part here like pnpm --help exec does?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drop it

@zkochan
Copy link
Member

zkochan commented Jan 30, 2022

An alternative (probably easier to implement) solution would be to escape everything after run.

So you could only pass options to run before the run keyword.

This would work: pnpm -r run foo.
This would not work: pnpm run -r foo.
This would also not work: pnpm -r foo.

@gluxon gluxon force-pushed the v7-run-args-optional-double-dash branch from c575e75 to 098bc93 Compare January 30, 2022 17:51
@gluxon
Copy link
Member Author

gluxon commented Jan 30, 2022

That would be much easier to implement. I had an initial approach last week that just added run to this array:

escapeArgs: ['dlx', 'exec'],

It did end up confusing me that pnpm run --silent echo test didn't work as expected though. (So your 2nd bullet point.) Passing --silent in between run and before the script name works in Yarn for what it's worth, and I do think that behavior is better from a user experience perspective.

Are the implementation complexities reasonable given the improved UX of this approach? I personally believe so, but happy for you to make that call.

@zkochan
Copy link
Member

zkochan commented Jan 30, 2022

Passing --silent in between run and before the script name works in Yarn for what it's worth, and I do think that behavior is better from a user experience perspective.

OK, if it is supported by Yarn, then we don't really have a choice. We need to support it as well.

@zkochan zkochan merged commit c35ac78 into pnpm:v7 Jan 30, 2022
@zkochan zkochan added this to the v7.0 milestone Jan 30, 2022
@zkochan zkochan mentioned this pull request Jan 30, 2022
1 task
@gluxon gluxon deleted the v7-run-args-optional-double-dash branch January 30, 2022 20:30
@gluxon
Copy link
Member Author

gluxon commented Jan 30, 2022

Thanks for taking a look so quickly!

OK, if it is supported by Yarn, then we don't really have a choice. We need to support it as well.

I am a bit surprised by this. Although in this case I do like yarn's behavior, I would hope pnpm deviates in other situations we think it can do better. I think you should feel free to make those calls @zkochan, even if individuals like me disagree. 🙂

@zkochan
Copy link
Member

zkochan commented Jan 30, 2022

I would love to but then we'll have a lot of issues from users that switch from Yarn. So it is easier just to support it.

@pnpm pnpm locked as resolved and limited conversation to collaborators Jul 20, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants