Skip to content

Fix environment related warnings #7684

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 9 commits into from
Jun 21, 2024
Merged

Fix environment related warnings #7684

merged 9 commits into from
Jun 21, 2024

Conversation

rauhul
Copy link
Member

@rauhul rauhul commented Jun 18, 2024

Adds a new Environment type to replace TSCBasics EnvironmentVariables
and ProcessEnvironmentBlock types. Updates points of use of the older
APIs with the newer API and adds unit tests for Environment.

Copy link
Contributor

@MaxDesiatov MaxDesiatov left a comment

Choose a reason for hiding this comment

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

I'm not convinced Environment warrants its own module, I'd prefer to keep it in Basics for now. It's unclear to me how that separation logic should be followed if at all. There's a lot of functionality in Basics, a new module for each type (or functionality aspect) would require a dozen of new modules, which IMO overcomplicates things.

@MaxDesiatov
Copy link
Contributor

MaxDesiatov commented Jun 19, 2024

If you insist on moving it out of Basics, it could be kept in Basics here as a stopgap measure. After AsyncProcess PR is merged, Environment could be moved to that new AsyncProcess module.

@xedin
Copy link
Contributor

xedin commented Jun 19, 2024

It seems like AsyncProcess module is indeed a good place for this type.

@MaxDesiatov
Copy link
Contributor

Main problem with that is a dependency on AbsolutePath in the AsyncProcess type itself. For the sake of not opening that can of modularization worms, I'd prefer this PR to be focused on fixing the actual warnings only. We can come back to modularization issues separately. This would also make PRs smaller and easier to review.

@rauhul rauhul marked this pull request as ready for review June 19, 2024 21:40
@rauhul rauhul enabled auto-merge (squash) June 19, 2024 21:40
@rauhul
Copy link
Member Author

rauhul commented Jun 19, 2024

@swift-ci test

@rauhul
Copy link
Member Author

rauhul commented Jun 19, 2024

@swift-ci test windows

@MaxDesiatov MaxDesiatov requested a review from compnerd June 19, 2024 23:59
@MaxDesiatov
Copy link
Contributor

MaxDesiatov commented Jun 20, 2024

@compnerd would you have a moment to look how well this works for Windows compatibility?

}
}

// FIXME: This should come from Foundation
Copy link
Member

Choose a reason for hiding this comment

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

I don't think that this can come from Foundation. Foundation's API is not easy to change and the environment being case insensitive is not currently well handled there.

Copy link
Member Author

Choose a reason for hiding this comment

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

The intent of the comment that this type should come from a common Swift ecosystem library and foundation seems like the most natural place.

}
}

package static var pathValueDelimiter: String {
Copy link
Member

Choose a reason for hiding this comment

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

I think pathEntryDelimiter is better - the variable is a set of key, value pairs which makes value a bit overloaded.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I can change the name to your suggestion

package static func set(key: EnvironmentKey, value: String?) throws {
if let value = value {
#if os(Windows)
guard _putenv("\(key)=\(value)") == 0 else {
Copy link
Member

Choose a reason for hiding this comment

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

This is annoying - I don't know if we should use _putenv or SetEnvironmentVariableW. The former only adjusts the "C" functions view, the latter only changes the Win32 view. I suppose that we could call both?

Copy link
Member Author

Choose a reason for hiding this comment

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

I can have it call both, in general I strongly dislike mutating the global environment anyways

Comment on lines 283 to 294
for (key, value) in sorted {
if first {
first = false
} else {
description.append(", ")
}
description.append("\"")
description.append(key.rawValue)
description.append("=")
description.append(value)
description.append("\"")
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
for (key, value) in sorted {
if first {
first = false
} else {
description.append(", ")
}
description.append("\"")
description.append(key.rawValue)
description.append("=")
description.append(value)
description.append("\"")
}
sorted.map { #""\#($0.rawValue)"="\#($1)""# }.joined(", ")

Copy link
Member Author

Choose a reason for hiding this comment

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

I avoided string interpolation and join to avoid intermediate allocations

Copy link
Member Author

Choose a reason for hiding this comment

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

That said it really doesn't matter and I can use an easier to read method

Copy link
Contributor

Choose a reason for hiding this comment

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

Curious if we have issues on the compiler repo tracking possible optimizer work that hypothetically could get rid of intermediate allocations here?

Copy link
Contributor

Choose a reason for hiding this comment

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

Curious if we have issues on the compiler repo tracking possible optimizer work that hypothetically could get rid of intermediate allocations here?

Copy link
Contributor

@MaxDesiatov MaxDesiatov left a comment

Choose a reason for hiding this comment

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

Thank you for making SwiftPM more environment friendly! 🙈

@MaxDesiatov MaxDesiatov requested a review from compnerd June 20, 2024 23:28
@MaxDesiatov
Copy link
Contributor

@swift-ci test windows

@rauhul
Copy link
Member Author

rauhul commented Jun 20, 2024

@swift-ci test

@rauhul
Copy link
Member Author

rauhul commented Jun 20, 2024

@swift-ci test windows

1 similar comment
@rauhul
Copy link
Member Author

rauhul commented Jun 21, 2024

@swift-ci test windows

@rauhul
Copy link
Member Author

rauhul commented Jun 21, 2024

looks like windows CI is failing to checkout Swift which is odd because the other checks are working

@rauhul
Copy link
Member Author

rauhul commented Jun 21, 2024

@swift-ci test windows

1 similar comment
@MaxDesiatov
Copy link
Contributor

@swift-ci test windows

Copy link
Contributor

@xedin xedin left a comment

Choose a reason for hiding this comment

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

Looks great, thank you! Could you please run swiftformat on new files before merging?

@rauhul
Copy link
Member Author

rauhul commented Jun 21, 2024

@swift-ci test

@rauhul
Copy link
Member Author

rauhul commented Jun 21, 2024

@swift-ci test windows

@rauhul rauhul merged commit 74aea87 into main Jun 21, 2024
5 checks passed
@rauhul rauhul deleted the fix-warnings branch June 21, 2024 21:48
MaxDesiatov pushed a commit that referenced this pull request Jun 27, 2024
Adds a new `Environment` type to replace TSCBasics EnvironmentVariables
and ProcessEnvironmentBlock types. Updates points of use of the older
APIs with the newer API and adds unit tests for `Environment`.

(cherry picked from commit 74aea87)
MaxDesiatov added a commit that referenced this pull request Jun 27, 2024
Includes these PRs cherry-picked off `main`
* #7605
* #7660
* #7667
* #7682
* #7687
* #7690
* #7684
* #7679

**Explanation**: Cherry-pick of recent NFC changes, which makes it
easier to cherry-pick actual bug fixes onto 6.0 due to the reduced
number of merge conflicts.
**Scope**: broad, includes both modules graph and llbuild-related
changes.
**Risk**: low, the test suite is passing, no functional changes are
included, and cherry-picked changes were incubated on `main` for some
time.
**Testing**: Existing automated test suite.
**Issue**: N/A
**Reviewers**: @xedin @MaxDesiatov @rauhul

---------

Co-authored-by: Pavel Yaskevich <xedin@apache.org>
Co-authored-by: Danny Mösch <danny.moesch@icloud.com>
Co-authored-by: Rauhul Varma <rauhul@apple.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