-
Notifications
You must be signed in to change notification settings - Fork 208
Only make input paths absolute when -working-directory is passed #1708
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -45,12 +45,15 @@ private var testInputsPath: AbsolutePath { | |||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
func toPath(_ path: String, base: AbsolutePath = localFileSystem.currentWorkingDirectory!) throws -> VirtualPath { | ||||||||||
return try VirtualPath(path: path).resolvedRelativePath(base: base) | ||||||||||
func toPath(_ path: String, isRelative: Bool = true) throws -> VirtualPath { | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am slightly prefer leaving the tests in the new state but add Also, would be nice to add a test to check the behavior of path arguments with/without There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For the changed tests? For
I added |
||||||||||
if isRelative { | ||||||||||
return VirtualPath.relative(try .init(validating: path)) | ||||||||||
} | ||||||||||
return try VirtualPath(path: path).resolvedRelativePath(base: localFileSystem.currentWorkingDirectory!) | ||||||||||
} | ||||||||||
|
||||||||||
func toPathOption(_ path: String, base: AbsolutePath = localFileSystem.currentWorkingDirectory!) throws -> Job.ArgTemplate { | ||||||||||
return .path(try toPath(path, base: base)) | ||||||||||
func toPathOption(_ path: String, isRelative: Bool = true) throws -> Job.ArgTemplate { | ||||||||||
return .path(try toPath(path, isRelative: isRelative)) | ||||||||||
} | ||||||||||
|
||||||||||
final class SwiftDriverTests: XCTestCase { | ||||||||||
|
@@ -351,7 +354,7 @@ final class SwiftDriverTests: XCTestCase { | |||||||||
]) | ||||||||||
XCTAssertEqual(driver.recordedInputModificationDates, [ | ||||||||||
.init(file: VirtualPath.absolute(main).intern(), type: .swift) : mainMDate, | ||||||||||
.init(file: VirtualPath.absolute(util).intern(), type: .swift) : utilMDate, | ||||||||||
.init(file: VirtualPath.relative(utilRelative).intern(), type: .swift) : utilMDate, | ||||||||||
]) | ||||||||||
} | ||||||||||
} | ||||||||||
|
@@ -1295,12 +1298,11 @@ final class SwiftDriverTests: XCTestCase { | |||||||||
let plannedJobs = try driver.planBuild().removingAutolinkExtractJobs() | ||||||||||
XCTAssertEqual(plannedJobs.count, 3) | ||||||||||
XCTAssertEqual(plannedJobs[0].kind, .compile) | ||||||||||
XCTAssertTrue(plannedJobs[0].primaryInputs.map{ $0.file.description }.elementsEqual([rebase("foo.swift"), | ||||||||||
rebase("bar.swift")])) | ||||||||||
XCTAssertTrue(plannedJobs[0].primaryInputs.map{ $0.file.description }.elementsEqual(["foo.swift", "bar.swift"])) | ||||||||||
XCTAssertTrue(plannedJobs[0].commandLine.contains("-emit-const-values-path")) | ||||||||||
XCTAssertEqual(plannedJobs[0].outputs.filter({ $0.type == .swiftConstValues }).count, 2) | ||||||||||
XCTAssertEqual(plannedJobs[1].kind, .compile) | ||||||||||
XCTAssertTrue(plannedJobs[1].primaryInputs.map{ $0.file.description }.elementsEqual([rebase("baz.swift")])) | ||||||||||
XCTAssertTrue(plannedJobs[1].primaryInputs.map{ $0.file.description }.elementsEqual(["baz.swift"])) | ||||||||||
XCTAssertTrue(plannedJobs[1].commandLine.contains("-emit-const-values-path")) | ||||||||||
XCTAssertEqual(plannedJobs[1].outputs.filter({ $0.type == .swiftConstValues }).count, 1) | ||||||||||
XCTAssertEqual(plannedJobs[2].kind, .link) | ||||||||||
|
@@ -1331,13 +1333,12 @@ final class SwiftDriverTests: XCTestCase { | |||||||||
let plannedJobs = try driver.planBuild().removingAutolinkExtractJobs() | ||||||||||
XCTAssertEqual(plannedJobs.count, 3) | ||||||||||
XCTAssertEqual(plannedJobs[0].kind, .compile) | ||||||||||
XCTAssertTrue(plannedJobs[0].primaryInputs.map{ $0.file.description }.elementsEqual([rebase("foo.swift"), | ||||||||||
rebase("bar.swift")])) | ||||||||||
XCTAssertTrue(plannedJobs[0].primaryInputs.map{ $0.file.description }.elementsEqual(["foo.swift", "bar.swift"])) | ||||||||||
XCTAssertTrue(plannedJobs[0].commandLine.contains(subsequence: [.flag("-emit-const-values-path"), .path(.absolute(try .init(validating: "/tmp/foo.build/foo.swiftconstvalues")))])) | ||||||||||
XCTAssertTrue(plannedJobs[0].commandLine.contains(subsequence: [.flag("-emit-const-values-path"), .path(.absolute(try .init(validating: "/tmp/foo.build/bar.swiftconstvalues")))])) | ||||||||||
XCTAssertEqual(plannedJobs[0].outputs.filter({ $0.type == .swiftConstValues }).count, 2) | ||||||||||
XCTAssertEqual(plannedJobs[1].kind, .compile) | ||||||||||
XCTAssertTrue(plannedJobs[1].primaryInputs.map{ $0.file.description }.elementsEqual([rebase("baz.swift")])) | ||||||||||
XCTAssertTrue(plannedJobs[1].primaryInputs.map{ $0.file.description }.elementsEqual(["baz.swift"])) | ||||||||||
XCTAssertTrue(plannedJobs[1].commandLine.contains("-emit-const-values-path")) | ||||||||||
XCTAssertEqual(plannedJobs[1].outputs.filter({ $0.type == .swiftConstValues }).count, 1) | ||||||||||
XCTAssertTrue(plannedJobs[1].commandLine.contains(subsequence: [.flag("-emit-const-values-path"), .path(.absolute(try .init(validating: "/tmp/foo.build/baz.swiftconstvalues")))])) | ||||||||||
|
@@ -3039,10 +3040,10 @@ final class SwiftDriverTests: XCTestCase { | |||||||||
XCTAssertEqual(emitModuleJob.outputs[0].file, try toPath("Test.swiftmodule")) | ||||||||||
XCTAssertEqual(emitModuleJob.outputs[1].file, try toPath("Test.swiftdoc")) | ||||||||||
XCTAssertEqual(emitModuleJob.outputs[2].file, try toPath("Test.swiftsourceinfo")) | ||||||||||
XCTAssertEqual(emitModuleJob.outputs[3].file, try toPath("Test.swiftinterface")) | ||||||||||
XCTAssertEqual(emitModuleJob.outputs[3].file, try VirtualPath(path: "./Test.swiftinterface")) | ||||||||||
XCTAssertEqual(emitModuleJob.outputs[4].file, try toPath("Test.private.swiftinterface")) | ||||||||||
XCTAssertEqual(emitModuleJob.outputs[5].file, try toPath("Test-Swift.h")) | ||||||||||
XCTAssertEqual(emitModuleJob.outputs[6].file, try toPath("Test.tbd")) | ||||||||||
XCTAssertEqual(emitModuleJob.outputs[6].file, try VirtualPath(path: "./Test.tbd")) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @rmaz do you recall why this change was needed for only these specific outputs? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't, but changing it to
So somewhere a CWD of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After some code wandering, it is because the path is unspecified in the compiler args, so falls back to this codepath to determine it: swift-driver/Sources/SwiftDriver/Driver/Driver.swift Lines 3623 to 3626 in eba1952
This ends up calling This ends up calculating the parent as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you! |
||||||||||
if driver1.targetTriple.isDarwin { | ||||||||||
XCTAssertEqual(emitModuleJob.outputs[7].file, try toPath("Test.abi.json")) | ||||||||||
} | ||||||||||
|
@@ -5188,7 +5189,7 @@ final class SwiftDriverTests: XCTestCase { | |||||||||
// Reset the temporary store to ensure predictable results. | ||||||||||
VirtualPath.resetTemporaryFileStore() | ||||||||||
var driver = try Driver(args: [ | ||||||||||
"swiftc", "-emit-executable", "test.swift", "-emit-module", "-avoid-emit-module-source-info", "-experimental-emit-module-separately" | ||||||||||
"swiftc", "-emit-executable", "test.swift", "-emit-module", "-avoid-emit-module-source-info", "-experimental-emit-module-separately", "-working-directory", localFileSystem.currentWorkingDirectory!.description | ||||||||||
]) | ||||||||||
let plannedJobs = try driver.planBuild() | ||||||||||
|
||||||||||
|
@@ -7234,6 +7235,83 @@ final class SwiftDriverTests: XCTestCase { | |||||||||
}) | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
func testRelativeInputs() throws { | ||||||||||
do { | ||||||||||
// Inputs with relative paths with no -working-directory flag should remain relative | ||||||||||
var driver = try Driver(args: ["swiftc", | ||||||||||
"-target", "arm64-apple-ios13.1", | ||||||||||
"foo.swift"]) | ||||||||||
let plannedJobs = try driver.planBuild() | ||||||||||
let compileJob = plannedJobs[0] | ||||||||||
XCTAssertEqual(compileJob.kind, .compile) | ||||||||||
XCTAssertTrue(compileJob.commandLine.contains(subsequence: ["-primary-file", try toPathOption("foo.swift", isRelative: true)])) | ||||||||||
} | ||||||||||
|
||||||||||
do { | ||||||||||
// Inputs with relative paths with -working-directory flag should prefix all inputs | ||||||||||
var driver = try Driver(args: ["swiftc", | ||||||||||
"-target", "arm64-apple-ios13.1", | ||||||||||
"foo.swift", | ||||||||||
"-working-directory", "/foo/bar"]) | ||||||||||
let plannedJobs = try driver.planBuild() | ||||||||||
let compileJob = plannedJobs[0] | ||||||||||
XCTAssertEqual(compileJob.kind, .compile) | ||||||||||
XCTAssertTrue(compileJob.commandLine.contains(subsequence: ["-primary-file", try toPathOption("/foo/bar/foo.swift", isRelative: false)])) | ||||||||||
} | ||||||||||
|
||||||||||
try withTemporaryFile { fileMapFile in | ||||||||||
let outputMapContents: ByteString = """ | ||||||||||
{ | ||||||||||
"": { | ||||||||||
"diagnostics": "/tmp/foo/.build/x86_64-apple-macosx/debug/foo.build/master.dia", | ||||||||||
"emit-module-diagnostics": "/tmp/foo/.build/x86_64-apple-macosx/debug/foo.build/master.emit-module.dia" | ||||||||||
}, | ||||||||||
"foo.swift": { | ||||||||||
"object": "/tmp/foo/.build/x86_64-apple-macosx/debug/foo.build/foo.o" | ||||||||||
} | ||||||||||
} | ||||||||||
""" | ||||||||||
try localFileSystem.writeFileContents(fileMapFile.path, bytes: outputMapContents) | ||||||||||
|
||||||||||
// Inputs with relative paths should be found in output file maps | ||||||||||
var driver = try Driver(args: ["swiftc", | ||||||||||
"-target", "arm64-apple-ios13.1", | ||||||||||
"foo.swift", | ||||||||||
"-output-file-map", fileMapFile.path.description]) | ||||||||||
let plannedJobs = try driver.planBuild() | ||||||||||
let compileJob = plannedJobs[0] | ||||||||||
XCTAssertEqual(compileJob.kind, .compile) | ||||||||||
XCTAssertTrue(compileJob.commandLine.contains(subsequence: ["-o", try toPathOption("/tmp/foo/.build/x86_64-apple-macosx/debug/foo.build/foo.o", isRelative: false)])) | ||||||||||
} | ||||||||||
|
||||||||||
try withTemporaryFile { fileMapFile in | ||||||||||
let outputMapContents: ByteString = """ | ||||||||||
{ | ||||||||||
"": { | ||||||||||
"diagnostics": "/tmp/foo/.build/x86_64-apple-macosx/debug/foo.build/master.dia", | ||||||||||
"emit-module-diagnostics": "/tmp/foo/.build/x86_64-apple-macosx/debug/foo.build/master.emit-module.dia" | ||||||||||
}, | ||||||||||
"/some/workingdir/foo.swift": { | ||||||||||
"object": "/tmp/foo/.build/x86_64-apple-macosx/debug/foo.build/foo.o" | ||||||||||
} | ||||||||||
} | ||||||||||
""" | ||||||||||
try localFileSystem.writeFileContents(fileMapFile.path, bytes: outputMapContents) | ||||||||||
|
||||||||||
// Inputs with relative paths and working-dir should use absolute paths in output file maps | ||||||||||
var driver = try Driver(args: ["swiftc", | ||||||||||
"-target", "arm64-apple-ios13.1", | ||||||||||
"foo.swift", | ||||||||||
"-working-directory", "/some/workingdir", | ||||||||||
"-output-file-map", fileMapFile.path.description]) | ||||||||||
let plannedJobs = try driver.planBuild() | ||||||||||
let compileJob = plannedJobs[0] | ||||||||||
XCTAssertEqual(compileJob.kind, .compile) | ||||||||||
XCTAssertTrue(compileJob.commandLine.contains(subsequence: ["-o", try toPathOption("/tmp/foo/.build/x86_64-apple-macosx/debug/foo.build/foo.o", isRelative: false)])) | ||||||||||
} | ||||||||||
|
||||||||||
} | ||||||||||
|
||||||||||
func testRelativeResourceDir() throws { | ||||||||||
do { | ||||||||||
|
@@ -7274,7 +7352,7 @@ final class SwiftDriverTests: XCTestCase { | |||||||||
XCTAssertEqual(compileJob.kind, .compile) | ||||||||||
let linkJob = plannedJobs[1] | ||||||||||
XCTAssertEqual(linkJob.kind, .link) | ||||||||||
XCTAssertTrue(linkJob.commandLine.contains(try toPathOption(sdkRoot.pathString + "/usr/lib/swift/linux/x86_64/swiftrt.o"))) | ||||||||||
XCTAssertTrue(linkJob.commandLine.contains(try toPathOption(sdkRoot.pathString + "/usr/lib/swift/linux/x86_64/swiftrt.o", isRelative: false))) | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe the code will be cleaner (save all the
?? fileSystem.currentWorkingDirectory
) ifself.workingDirectory
is always set regardless of if-working-directory
is passed but only callsapplyWorkingDirectory
if the option is passed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried that initially, but then the output paths (and profile data paths) end up absolute. I didn't track down where that was happening, but there seems to be multiple places in the code that would end up prefixing paths if
workingDirectory
was set.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe driver will think that working-directory is passed via command-line so it is passing working directory (in abs path) frontend flag plus some other flags derived from working directory?
Sounds fine with me.