Skip to content

[windows] Avoid %r for quoting module-cache-path in Windows. #34176

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

Merged
merged 1 commit into from
Oct 10, 2020

Conversation

drodriguez
Copy link
Contributor

%r returns a representation of the object that is valid Python syntax.
For numbers and strings this representation is very compatible with
Unix shells, but for Windows, the quoting performed by Python will not
be compatible. For example the Windows path separator \ will be
escaped as \\, which is not necessary for Windows and might make
some tests that try to match path fail.

Python 3.3 has shlex.quote, which was previously available as
pipes.quote in earlier Python versions. pipes.quote was indeed
used in several points of the lit.cfg file. For Windows, one need to
do their own quoting.

This change introduces shell_quote which uses shlex.quote or
pipes.quote in Unix depending on the Python version, and provides an
implementation of shell_quote for Windows. It replaces every usage
of pipes.quotes for shell_quote, and modifies the value of
mcp_opt to use shell_quote and not %r.

This should fix the test Driver\working-directory.swift in the
Windows Visual Studio 2017 builder.

@drodriguez drodriguez requested a review from compnerd October 4, 2020 02:17
@drodriguez
Copy link
Contributor Author

@swift-ci please smoke test

@drodriguez
Copy link
Contributor Author

I might replace every usage of %r at some point, but for now, let's introduce only one change at the time.

Copy link
Member

@compnerd compnerd left a comment

Choose a reason for hiding this comment

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

The windows path makes me sad :-(. Thank you for cleaning this up. Removing the %r usage sounds like a good idea to me.

@compnerd
Copy link
Member

compnerd commented Oct 4, 2020

Seems that re.replace doesn't work with the windows builders?

@drodriguez drodriguez force-pushed the windows-percent-r-is-bad branch from 7526b2d to 26c436c Compare October 4, 2020 05:56
@drodriguez
Copy link
Contributor Author

Sorry. I run it in several of the test directories, but not all of them. It should have been s.replace(…) not re.replace(…).

@swift-ci please smoke test

@drodriguez
Copy link
Contributor Author

@swift-ci please test Windows platform

@drodriguez
Copy link
Contributor Author

The problem now is that because the \ are not longer escaped. There are usages of Lit's SubstituteCaptures (https://github.com/apple/llvm-project/blob/1a6659f0e7add3f1c1f5352fbf5b9811bac1adfc/llvm/utils/lit/lit/TestingConfig.py#L167) that use string interpolation with the non-escaped values. Because those strings end up as replacement strings of a regular expression, they have to be escaped for the replacement string (easy to do with .replace('\\', '\\\\')).

But then the working-directory.swift test starts to fail for some reason (FML). It seems that the matching is performed against some output of the Swift driver, which outputs the jobs command lines escaped, but probably escaped for Unix. I have to circle back here. The fix for one particular test might be easier than this, even if this should be good in the long run.

I don't know, however, if the card castle of escaping and unescaping can ever be solved.

PD: I still don't understand why it works in Windows VS2019.

@drodriguez
Copy link
Contributor Author

So I think I understand why it works in the VS 2019 nodes: they have Python 3.7.5 installed (comes from Visual Studio from reading the logs), while the VS 2017 nodes have 3.8.5 (installed with Chocolatey).

For os.path.realpath the documentation says "Changed in version 3.8: Symbolic links and junctions are now resolved on Windows.". When LLVM Lit starts it does a realpath on the lit configuration path which gives T:\swift\test-windows-x86_64 in Python 3.7.5, but C:\jenkins\workspace\oss-swift-windows-x86_64\build\swift\test-windows-x86_64 in Python 3.8.5.

And then enter PathSanitizingFileCheck into the game: it provides both SOURCE_DIR and BUILD_DIR, but BUILD_DIR value comes from CMake, which doesn't do any realpath shenanigans, so when Python 3.8.5 uses paths like C:\jenkins\workspace\oss-swift-windows-x86_64\build\swift\test-windows-x86_64 as the build directory, PathSanitizingFileCheck still uses T:\swift as prefix, and do not matches both.

To put the cherry on top, if one uses realpath for the substitutions of BUILD_DIR, PathSanitizingFileCheck stops working because those paths where always using forward slashes, and realpath changes everything to forward slashes.

Since this problem will start appearing when the VS 2019 nodes update to Python 3.8.5, I don't think the solution is going back to 3.7.5 in the VS 2017 nodes, but the solution is not going to be pretty.

@drodriguez drodriguez force-pushed the windows-percent-r-is-bad branch from 26c436c to bb9660d Compare October 4, 2020 20:30
@drodriguez
Copy link
Contributor Author

drodriguez commented Oct 4, 2020

@swift-ci please test Windows platform

Edit: https://ci-external.swift.org/view/Pull%20Request/job/swift-PR-windows/7663/ but not in GitHub UI yet.

@drodriguez
Copy link
Contributor Author

@swift-ci please smoke test

@drodriguez drodriguez force-pushed the windows-percent-r-is-bad branch from bb9660d to 0326269 Compare October 4, 2020 21:34
@drodriguez
Copy link
Contributor Author

drodriguez commented Oct 4, 2020

@drodriguez
Copy link
Contributor Author

@swift-ci please smoke test

@compnerd
Copy link
Member

compnerd commented Oct 4, 2020

os.normpath should convert the path to the canonical spelling for the platform - that is / will be converted to \ which might be what needs to be applied here?

@drodriguez
Copy link
Contributor Author

drodriguez commented Oct 5, 2020

Do not merge yet. The Windows CI failed badly. I have to look and figure out what's happening in that machine (it is quite hard to get things right because I have to copy code back and forth between machines).

os.normpath will work a little better, but I don't think changing LLVM Lit's is the way forward. Besides that, I think realpath has its benefits in this case.

@compnerd
Copy link
Member

compnerd commented Oct 7, 2020

@swift-ci please test Windows platform

%r returns a representation of the object that is valid Python syntax.
For numbers and strings this representation is very compatible with
Unix shells, but for Windows, the quoting performed by Python will not
be compatible. For example the Windows path separator `\` will be
escaped as `\\`, which is not necessary for Windows and might make
some tests that try to match path fail.

Python 3.3 has `shlex.quote`, which was previously available as
`pipes.quote` in earlier Python versions. `pipes.quote` was indeed
used in several points of the `lit.cfg` file. For Windows, one need to
do their own quoting.

This change introduces `shell_quote` which uses `shlex.quote` or
`pipes.quote` in Unix depending on the Python version, and provides an
implementation of `shell_quote` for Windows. It replaces every usage
of `pipes.quotes` for `shell_quote`, and modifies the value of
`mcp_opt` to use `shell_quote` and not `%r`.

This should fix the test `Driver\working-directory.swift` in the
Windows Visual Studio 2017 builder.
@drodriguez drodriguez force-pushed the windows-percent-r-is-bad branch from 0326269 to 8470a66 Compare October 8, 2020 19:12
@drodriguez
Copy link
Contributor Author

drodriguez commented Oct 8, 2020

Removed the part that adds carets. Seems that Lit already does some massaging of the arguments, and the quoting I introduced was misbehaving with the one Lit implements. I tested having a space in my Python path, like it seems to happen in the build machine.

@swift-ci please test Windows platform

Edit: https://ci-external.swift.org/view/Pull%20Request/job/swift-PR-windows/7781/
Edit 2: And it is 🔵!

@drodriguez
Copy link
Contributor Author

@swift-ci please smoke test

@drodriguez
Copy link
Contributor Author

@swift-ci please smoke test Linux platform

@drodriguez
Copy link
Contributor Author

@swift-ci please test Linux platform

@swift-ci
Copy link
Contributor

swift-ci commented Oct 9, 2020

Build failed
Swift Test Linux Platform
Git Sha - 8470a66

@drodriguez
Copy link
Contributor Author

@swift-ci please test Linux platform

@drodriguez drodriguez merged commit 169948f into swiftlang:main Oct 10, 2020
@drodriguez drodriguez deleted the windows-percent-r-is-bad branch October 10, 2020 15:49
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.

3 participants