Skip to content

code_format: fix path matching#20108

Merged
phlax merged 2 commits into
envoyproxy:mainfrom
DavidPeet8:format-fix
Mar 4, 2022
Merged

code_format: fix path matching#20108
phlax merged 2 commits into
envoyproxy:mainfrom
DavidPeet8:format-fix

Conversation

@DavidPeet8
Copy link
Copy Markdown
Contributor

Commit Message: This change fixes an issue in the code formatter where explicitly
excluded paths are incorrectly matched when the script is provided
arguments that do not start with './' or that end with a trailing
slash.

To address this issue this change normalizes paths before checking
if the path is excluded.

Additional Description: I think the core issue here is the use of direct string matching for excluded files. Refactoring to address the root cause is out of scope for this change, but will likely need to be done in the future.

Risk Level: low

Testing: Ran code format with arguments of the form 'path/to/dir' and './path/to/dir/', verifying that excluded files are not formatted as expected.

Docs Changes: n/a
Release Notes: n/a
Platform Specific Features: n/a
Fixes: #20077

This change fixes an issue in the code formatter where explicitly
excluded paths are incorrectly matched when the script is provided
arguments that do not start with './' or that end with a trailing
slash.

To address this issue this change normalizes paths before checking
if the path is excluded.

Signed-off-by: David Peet <davidpeet@tutanota.com>
@repokitteh-read-only
Copy link
Copy Markdown

Hi @DavidPeet8, welcome and thank you for your contribution.

We will try to review your Pull Request as quickly as possible.

In the meantime, please take a look at the contribution guidelines if you have not done so already.

🐱

Caused by: #20108 was opened by DavidPeet8.

see: more, trace.

@daixiang0
Copy link
Copy Markdown
Member

Thanks for your contribution!

Please wait for a maintainer to trigger CI flow.

/cc @envoyproxy/stable-maintainers

@phlax phlax self-assigned this Feb 25, 2022
Copy link
Copy Markdown
Contributor

@yanavlasov yanavlasov left a comment

Choose a reason for hiding this comment

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

/wait-any

def normalize_path(path):
"""Convert path to form ./path/to/dir/ for directories and ./path/to/file otherwise"""
isdir = os.path.isdir(args.target_path)
if not path.startswith("./"):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Would it make sense to prepend "./" before checking if it is a directory? Just in case.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

im wondering if pathlib could be helpful here - it does quite a bit of path normalization - altho it seems like elsewhere is interpreting the appended / so maybe that just complicates things

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Would it make sense to prepend "./" before checking if it is a directory? Just in case.

@yanavlasov - Good point, calling this with an absolute path would lead to unexpected behaviour

im wondering if pathlib could be helpful here - it does quite a bit of path normalization - altho it seems like elsewhere is interpreting the appended / so maybe that just complicates things

@phlax - I think in the future something like pathlib could be quite helpful, but I think the introduction of path normalization via pathlib or some other library function should come with a larger refactor in this file. In particular, I think a diff changing to normalize via a library would be a good opportunity to refactor how paths are handled in general across the formatter.

As you mentioned, the trailing slash becomes a bit awkward to deal with as we would be modifying the path after it is normalized - hopefully this could be solved with such an additional refactor

Signed-off-by: David Peet <davidpeet@tutanota.com>
@DavidPeet8
Copy link
Copy Markdown
Contributor Author

@phlax - Are you able to trigger the pending workflow?

Copy link
Copy Markdown
Member

@phlax phlax left a comment

Choose a reason for hiding this comment

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

lgtm, thanks @DavidPeet8 for working through this

@phlax phlax merged commit 1f0feac into envoyproxy:main Mar 4, 2022
dubious90 pushed a commit to envoyproxy/nighthawk that referenced this pull request Mar 8, 2022
- no changes in `.bazelrc`, `.bazelversion`, `ci/run_envoy_docker.sh` or `tools/gen_compilation_database.py`.
- introducing a workaround for our use of the Envoy's `check_format.py` after envoyproxy/envoy#20108. See #815 for details.

Signed-off-by: Jakub Sobon <mumak@google.com>
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.

4 participants