Skip to content

Commit d317f54

Browse files
authored
Test parent directory of .netrc file is writable before using it (#4075)
* Test parent directory of .netrc file is writable before using it https://ci.swift.org/job/swift-PR-source-compat-suite-debug/4058/ ``` $ sandbox-exec -f /Users/buildnode/jenkins/workspace-private/swift-source-compat-suite-sandbox/sandbox_package.sb /Users/buildnode/jenkins/workspace-private/swift-PR-source-compat-suite-debug/build/compat_macos/install/toolchain/usr/bin/swift package --disable-sandbox -C /Users/buildnode/jenkins/workspace/swift-PR-source-compat-suite-debug/swift-source-compat-suite/project_cache/GRDB.swift clean warning: '--chdir/-C' option is deprecated; use '--package-path' instead warning: Failed to load .netrc file at /Users/buildnode/.netrc. Error: unreadableFile(<AbsolutePath:"/Users/buildnode/.netrc">) error: other(1) ``` Not sure if the `other(1)` error is `.netrc` related. Trying to see if this code change will fix it. rdar://88308095 * Make sure defaultDirectory is not nil before comparing sharedConfigurationDirectory sharedConfigurationDirectory is a custom path iff defaultDirectory is non nil (in this case it's only ever nil when an error occurs while reading/writing the directory) and defaultDirectory != sharedConfigurationDirectory
1 parent f517ff5 commit d317f54

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

Sources/Commands/SwiftTool.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,13 @@ public class SwiftTool {
609609
// User didn't tell us to use these .netrc files so be more lenient with errors
610610
func loadNetrcNoThrows(at path: AbsolutePath) -> NetrcAuthorizationProvider? {
611611
guard localFileSystem.exists(path) else { return nil }
612+
613+
do {
614+
try withTemporaryFile(dir: path.parentDirectory) { _ in }
615+
} catch {
616+
self.observabilityScope.emit(warning: "\(path.parentDirectory) is not accessible or not writable, not using .netrc file in it: \(error)")
617+
return nil
618+
}
612619

613620
do {
614621
return try NetrcAuthorizationProvider(path: path, fileSystem: localFileSystem)

Sources/Workspace/Workspace.swift

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4085,9 +4085,10 @@ extension Workspace.Location {
40854085

40864086
// check that shared configuration directory is accessible, or warn + reset if not
40874087
if let sharedConfigurationDirectory = self.sharedConfigurationDirectory {
4088-
// it may not always be possible to create default location (for example de to restricted sandbox)
4088+
// It may not always be possible to create default location (for example de to restricted sandbox),
4089+
// in which case defaultDirectory would be nil.
40894090
let defaultDirectory = try? fileSystem.getOrCreateSwiftPMConfigurationDirectory(warningHandler: warningHandler)
4090-
if sharedConfigurationDirectory != defaultDirectory {
4091+
if defaultDirectory != nil, sharedConfigurationDirectory != defaultDirectory {
40914092
// custom location must be writable, throw if we cannot access it
40924093
try withTemporaryFile(dir: sharedConfigurationDirectory) { _ in }
40934094
} else {
@@ -4103,9 +4104,10 @@ extension Workspace.Location {
41034104

41044105
// check that shared configuration directory is accessible, or warn + reset if not
41054106
if let sharedSecurityDirectory = self.sharedSecurityDirectory {
4106-
// it may not always be possible to create default location (for example de to restricted sandbox)
4107+
// It may not always be possible to create default location (for example de to restricted sandbox),
4108+
// in which case defaultDirectory would be nil.
41074109
let defaultDirectory = try? fileSystem.getOrCreateSwiftPMSecurityDirectory()
4108-
if sharedSecurityDirectory != defaultDirectory {
4110+
if defaultDirectory != nil, sharedSecurityDirectory != defaultDirectory {
41094111
// custom location must be writable, throw if we cannot access it
41104112
try withTemporaryFile(dir: sharedSecurityDirectory) { _ in }
41114113
} else {
@@ -4121,9 +4123,10 @@ extension Workspace.Location {
41214123

41224124
// check that shared configuration directory is accessible, or warn + reset if not
41234125
if let sharedCacheDirectory = self.sharedCacheDirectory {
4124-
// it may not always be possible to create default location (for example de to restricted sandbox)
4126+
// It may not always be possible to create default location (for example de to restricted sandbox),
4127+
// in which case defaultDirectory would be nil.
41254128
let defaultDirectory = try? fileSystem.getOrCreateSwiftPMCacheDirectory()
4126-
if sharedCacheDirectory != defaultDirectory {
4129+
if defaultDirectory != nil, sharedCacheDirectory != defaultDirectory {
41274130
// custom location must be writable, throw if we cannot access it
41284131
try withTemporaryFile(dir: sharedCacheDirectory) { _ in }
41294132
} else {

0 commit comments

Comments
 (0)