Skip to content

commands/init: Make validation of positional arguments happen in the arguments package. Refactor away unneeded code.#38692

Open
SarahFrench wants to merge 3 commits into
mainfrom
remove-use-of-modulepath-func-in-init
Open

commands/init: Make validation of positional arguments happen in the arguments package. Refactor away unneeded code.#38692
SarahFrench wants to merge 3 commits into
mainfrom
remove-use-of-modulepath-func-in-init

Conversation

@SarahFrench
Copy link
Copy Markdown
Member

This PR refactors how unexpected positional arguments supplied to the init command are validated.

The ModulePath function is the result of how commands used to have positional DIR arguments, which allowed users to use a directory other than the directory where the CLI was being used. When that DIR argument was replaced with the -chdir flag, we end up with the code below. Whereas before it would use that argument to determine the path to the root module, now it just validates that the DIR argument isn't mistakenly being used and then returns details about the current working directory:

func ModulePath(args []string) (string, error) {
// TODO: test
if len(args) > 0 {
return "", fmt.Errorf("Too many command line arguments. Did you mean to use -chdir?")
}
path, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("Error getting pwd: %s", err)
}
return path, nil
}

It doesn't make sense to validate unexpected positional arguments here; that should be done in the arguments package.

This PR updates the code for parsing flags and arguments used with the init command to assert that no positional arguments are expected. If that validation passes, there are no args, then we don't need to return an empty list of args to the command implementation through the Args field removed in this PR.

Now that no args are being passed into the ModulePath function it's just a fancy way of calling os.Getwd. The Meta contains details about the working directory, so I've replaced use of the ModulePath function with the relevant method.

This is work towards deleting the ModulePath function from the codebase 💪🏻

Target Release

1.16.x

Rollback Plan

  • If a change needs to be reverted, we will roll out an update to the code within 7 days.

Changes to Security Controls

Are there any changes to security controls (access controls, encryption, logging) in this pull request? If so, explain.

CHANGELOG entry

  • This change is user-facing and I added a changelog entry.
  • This change is not user-facing.

…onal arguments.

This replaces validation performed by the `ModulePath` function.
There are no valid args to be passed around in the Args field. Now that validation of unexpected positional arguments is in place earlier this field is useless.
…leDir()

`ModulePath` is being used as a fancy/indirect way to use `os.Getwd`, and the data in (Meta).WorkingDir is equivalent to that.

When a command is run in the full CLI the (Meta).WorkingDir value is set to the current working dir (which may have been changed by a -chdir flag).
@SarahFrench SarahFrench added the no-changelog-needed Add this to your PR if the change does not require a changelog entry label Jun 5, 2026
@SarahFrench SarahFrench changed the title Remove use of modulepath func in init commands/init: Make validation of positional arguments happen in the arguments package. Refactor away unneeded code. Jun 5, 2026
@SarahFrench SarahFrench marked this pull request as ready for review June 5, 2026 18:08
@SarahFrench SarahFrench requested a review from a team as a code owner June 5, 2026 18:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-changelog-needed Add this to your PR if the change does not require a changelog entry

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant