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/breaking: issues with spawned procs, stdin use, env, output #56

Closed

Conversation

timcosta
Copy link

Several changes here, some of which are likely high urgency bugfixes, some of which are quality of life, and some are just cleanup. You may view this as a rewrite/major version, I probably would. Solving some of the issues necessitated larger structural changes.

Linting onType was entirely broken if you used a .sqlfluff config file anywhere but the workspace root, i.e deeper in the file tree. This was due to the usage of stdin for passing the file contents, which "hid" the file path tree from sqlfluff. I'll give an example because this one is a bit hard to understand:

  • Given a .sql file at /my/workspace/src/sql/blah.sql
  • Original command was basically: cd /my/workspace && sqlfluff lint --options - < "sql_string"
  • This broke because a .sqlfluff file in either /my/workspace/src or /my/workspace/src/sql that would be picked up by running the command in your terminal/in fix mode wouldn't be detected and merged in the root .sqlfluff file.

The list of changes is as follows:

  1. Removed the experimental format executeInTerminal option, all formats/fixes are executed in the terminal now due to the nested .sqlfluff issue
  2. Passing options.env to the child process was preventing sqlfluff from being located in the PATH, requiring you to hardcode the location always. It didn't seem like the old env setting was doing anything, so I removed it which allows the env to pass through
  3. There's now an output channel where users can see what's happening and why something isnt working as they may expect. It's pretty verbose, but due to the nature of sqlfluff errors (when they happen/what they mean/stack trace or not) it's a one-stop-shop if the editor doesnt give you proper output
  4. Most errors were silently swallowed with no feedback given to the user, all errors now provide some visual feedback for the user
  5. stdout/stderr from the lint/fix processes are captured and written to the output channel

This has been fully tested with both onType and onSave settings locally, however the tests in this project are not passing. They also aren't passing in master though so I wasnt too concerned about fixing that. It seems like a fairly involved change due to how the breakage is happening, I tried some basic fixes but I think it's going to require a bit of a test rewrite to handle the promise errors and some other bits like .sql files not being copied into the out dir the tests are run from.

If you don't want to merge this PR, that's absolutely fine, I understand that it's effectively a rewrite/breaking change. I will likely publish this PR on my own in that case though so that there's a version of the extension with the above features.

Comment on lines 78 to 80
public static lintBufferArguments(): string[] {
return ["lint", "--format", "json", "-"];
return ["--format", "json"];
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This can be removed and just use lintFileArguments instead as they now return the same values.

Copy link
Author

Choose a reason for hiding this comment

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

so, i was conflicted about this. the issue in my mind is that some people may have modified these already, and removing one might cause unintended side effects. i'm definitely more interested in cutting them down, so if thats the way you'd like to go happy to do so.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't quite follow? While these are in the configuration.ts file, they can't actually be modified by the user. These arguments should stay consistent. And it looks like you moved the "-" part of it to the section where you are evaluating if the linting should be completed with stdin or not. So we should be okay removing this and any places that use it could just use the lintFileArguments

}

public static formatBufferArguments(): string[] {
return ["fix", "--force", "-"];
return ["--force"];
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This can be removed as it is no longer used.

Comment on lines +28 to +31
if (Configuration.formatEnabled()) {
if (document.isDirty) {
await document.save();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

The reason the sqlfluff.experimental.format.executeInTerminal was used here and still marked as experimental was that using directly executing the file would result in the file getting overwritten on disk. This can lead to an error if the user changes the file while the formatting has not completed yet. Do you have any ideas for how to fix that bug?

this.process.kill('SIGKILL');
this.process = undefined;
}
const workspaceFolderRootPath = path.normalize(vscode.workspace.workspaceFolders[0].uri.fsPath);
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like we normalize the paths differently in the lint.ts and format.ts files. Could we change it, either here or in those files, so that we have consistent path normalization?

@RobertOstermann
Copy link
Contributor

These changes look good. I left a few comments and my main concern would be issues with executing all the formatting directly on the file instead of passing stdin when possible. The formatting can take some time and it is possible that the user will change the file while formatting is happening. When passing in stdin, the formatting is basically stopped and no changes are passed to the file. However, it is possible to automatically overwrite the file with changes instead of always getting an error message, so adding that note to the README might be sufficient.

I like the addition of logging everything to an output channel. That should help with identifying problems with the extension.

Don't worry about the testing not working, there are a few changes I need to make to the package.json scripts to ensure everything is added to the out directory correctly. And the actual tests probably need to be updated to ensure they work correctly.

One other note, could you run the eslint command eslint src --ext ts and fix any problems that pop up with eslint src --ext ts --fix. That helps keep the code formatting consistent.

@timcosta
Copy link
Author

I appreciate the quick feedback! I'll take a look at this tonight and have a revision for you tomorrow most likely. I have found a way to fix the experimental command so that it doesnt throw conflicts anymore, I didnt realize that was the issue but now that you mention it i've found a fix.

I'll also run the linting command, sorry my editor seems to have not picked up on the existence of a config to identify any issues while coding.

It's definitely not perfect when it comes to the timing of certain things like format start -> change made -> format finished as you mentioned, but I dont think there's a way around that without modifications to sqlfluff core unfortunately.

The ideal world is that sqlfluff core can accept multiple config files, or when it read from stdin it can also take a file path to do traversal on, but without that we're unfortunately pretty limited to "best of the bad" type of situations. IMO it's better to have a couple rough edges like the "race" condition mentioned than to ignore the nested sqlfluff config file features though, as that's likely to affect people in a more critical way than the race condition they can wait a second or two for.

@RobertOstermann
Copy link
Contributor

@timcosta Are you still planning on updating this PR so we can try to get these changes merged?

@timcosta
Copy link
Author

Yes, work has just had me slammed.

@RobertOstermann
Copy link
Contributor

RobertOstermann commented Aug 26, 2022

Yes, work has just had me slammed.

Ok, no rush.

@RobertOstermann
Copy link
Contributor

@timcosta I went ahead and made most of your changes and have merged #59 and released it. Version 0.1.0 should have those changes. I did not remove the executeInTerminal option as I wasn't sure how you planned to fix the race conditions.

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.

2 participants