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

fix(dotnet): should work with paths that contain spaces #392

Merged
merged 9 commits into from
Mar 3, 2022

Conversation

asinino
Copy link
Contributor

@asinino asinino commented Mar 2, 2022

Please review the code before merging it.

When a user has a project that contains a space in it's name the command
failed because it does not escaped the space to the shell command.
Fixes nx-dotnet#308
# Conflicts:
#	packages/dotnet/src/lib/core/dotnet.client.ts
@nx-cloud
Copy link

nx-cloud bot commented Mar 2, 2022

☁️ Nx Cloud Report

CI ran the following commands for commit 4ee665d. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this branch


✅ Successfully ran 5 targets

Sent with 💌 from NxCloud.

@AgentEnder
Copy link
Member

Hey @asinino, I've been looking over the code and I'm not sure why the format executor is failing on this branch. It seems like the isNet60OrHigher flag is set incorrectly, I'll look into this a bit

private execute(cmd: string): Buffer {
return execSync(cmd, { cwd: this.cwd || process.cwd() });
private execute(params: string[]): Buffer {
return spawnSync(this.cliCommand.command, params, {
Copy link
Member

Choose a reason for hiding this comment

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

Why are we swapping to spawnSync over execSync here? This is more complex maintainability-wise and shouldn't be needed.

Copy link
Member

Choose a reason for hiding this comment

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

I'm not totally opposed to spawnSync actually, some stuff does work out better this way.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, had a lengthy meeting couldn't look into this.

I converted to spawnSync to avoid converting all parameters to string first, spawnSync let us pass each parameter on the array so it is automatically escaped.
With execSync I had some problems passing on "Security Hotspot" tests because it allowed the user to pass arbitrary commands to execSync, like appending in the last parameter: "&& rm -rf *" this would delete everything on the current folder. Using the spawnSync command prevents the user to do this.

Copy link
Member

Choose a reason for hiding this comment

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

So with nx-plugins and other dev tooling in general, the hotspots that pop up from sonarqube can be mostly ignored. The reason being that those commands are being run by the developer, on their own machine. It's not really an attack point, as someone would already have to be able to run arbitrary commands to exercise it.

The solution for most things that come up like that, are just for me to hit the "not a problem" button while reviewing the PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That would be a lot easier because I had to change the method a lot, I'll take it into account next time, for this one I was kinda in a rush to have a deployable version because the previous one was broken.

return execSync(cmd, { cwd: this.cwd || process.cwd() });
private execute(params: string[]): Buffer {
return spawnSync(this.cliCommand.command, params, {
stdio: 'inherit',
Copy link
Member

Choose a reason for hiding this comment

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

Adding 'inherit' here causes the returned buffers to be empty

Copy link
Contributor Author

@asinino asinino Mar 3, 2022

Choose a reason for hiding this comment

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

I have a new solution for this have to discuss with you, I didn't merge it yet. This should resolve the problem:

  private execute(params: string[], stdio: StdioOptions = 'pipe'): Buffer {
    const proc = spawnSync(this.cliCommand.command, params, {
      stdio,
      cwd: this.cwd || process.cwd(),
    });

    let result = Buffer.alloc(0);
    if (proc?.stdout) {
      result = Buffer.concat([result, proc.stdout]);
    }
    if (proc?.stderr) {
      result = Buffer.concat([result, proc.stderr]);
    }
    return result;
  }

Copy link
Member

Choose a reason for hiding this comment

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

This would work, but its fine to duplicate the spawnSync call for this. The idea behind execute is that we are doing some kind of processing to the Buffer on our side, and we need it. If we are displaying the outputs of the command to the user, its the wrong method to use.

Copy link
Contributor Author

@asinino asinino Mar 4, 2022

Choose a reason for hiding this comment

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

I agree with different calls for spawnSync with different behavior, it's easier to not mess it up.

console.log(
`Executing Command: ${this.cliCommand.command} ${params.join(', ')}`,
);
return this.execute(params);
Copy link
Member

Choose a reason for hiding this comment

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

This method needs the 'stdio: inherit' so that things like nx build show terminal output.

@sonarcloud
Copy link

sonarcloud bot commented Mar 3, 2022

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@AgentEnder AgentEnder changed the title Fix/issue#308 - parameters are managed as an array fix(dotnet): should work with paths that contain spaces Mar 3, 2022
@AgentEnder AgentEnder merged commit fa86355 into nx-dotnet:master Mar 3, 2022
github-actions bot pushed a commit that referenced this pull request Mar 3, 2022
## [1.9.5](v1.9.4...v1.9.5) (2022-03-03)

### Bug Fixes

* **dotnet:** should work with paths that contain spaces ([#392](#392)) ([fa86355](fa86355))

Mar 3, 2022, 5:02 PM
@github-actions
Copy link
Contributor

github-actions bot commented Mar 3, 2022

🎉 This PR is included in version 1.9.5 🎉

The release is available on:

Your semantic-release bot 📦🚀

@github-actions
Copy link
Contributor

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 27, 2023
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