Skip to content
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

[SR-5076] Unable to set environment variables on Linux #4095

Closed
swift-ci opened this issue Jun 2, 2017 · 2 comments
Closed

[SR-5076] Unable to set environment variables on Linux #4095

swift-ci opened this issue Jun 2, 2017 · 2 comments

Comments

@swift-ci
Copy link
Contributor

swift-ci commented Jun 2, 2017

Previous ID SR-5076
Radar None
Original Reporter ylin (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment

Ubuntu 16.04, Swift 3.1.1

Additional Detail from JIRA
Votes 0
Component/s Foundation
Labels Bug
Assignee None
Priority Medium

md5: 4656dd62c0d34fa55d599a3fef91df61

Issue Description:

On macOS, I am able to set env vars via `setenv`:

ylin@youming-mbpr:~$ swift
Welcome to Apple Swift version 3.1 (swiftlang-802.0.53 clang-802.0.42). Type :help for assistance.
  1> import Foundation
  2> ProcessInfo.processInfo.environment
$R0: [String : String] = 2 key/value pairs {
  [0] = {
    key = "OS_ACTIVITY_DT_MODE"
    value = "enable"
  }
  [1] = {
    key = "__CF_USER_TEXT_ENCODING"
    value = "0x1F5:0x0:0x0"
  }
}
  3> setenv("TEST", "FOOBAR", 1)
$R1: Int32 = 0
  4> ProcessInfo.processInfo.environment
$R2: [String : String] = 3 key/value pairs {
  [0] = {
    key = "TEST"
    value = "FOOBAR"
  }
  [1] = {
    key = "OS_ACTIVITY_DT_MODE"
    value = "enable"
  }
  [2] = {
    key = "__CF_USER_TEXT_ENCODING"
    value = "0x1F5:0x0:0x0"
  }
}

However, on Ubuntu 16.04, I am not able to:

ylin@ylin-VirtualBox:~$ swift -I/home/ylin/.swiftenv/versions/3.1.1/usr/lib/swift/clang/include
Welcome to Swift version 3.1.1 (swift-3.1.1-RELEASE). Type :help for assistance.
  1> import Foundation
  2> ProcessInfo.processInfo.environment
$R0: [String : String] = 0 key/value pairs
  3> setenv("TEST", "FOOBAR", 1)
$R1: Int32 = 0
  4> ProcessInfo.processInfo.environment 
$R2: [String : String] = 0 key/value pairs

I want to be able to set custom env vars in unit tests in order to test features of a package that reads from env vars and be able to test on both macOS and Ubuntu.

@swift-ci
Copy link
Contributor Author

swift-ci commented Feb 9, 2018

Comment by Andrew Lees (JIRA)

New issue with more details raised here https://bugs.swift.org/browse/SR-6968

@spevans
Copy link
Collaborator

spevans commented Aug 15, 2018

This was fixed as part of SR-6968, and works in Swift 4.1:

$ cat sr_5076.swift 
import Foundation

print(ProcessInfo.processInfo.environment)
setenv("TEST", "FOOBAR", 1)
print(ProcessInfo.processInfo.environment)

$ env -i ~/swift-4.1-RELEASE-ubuntu16.04/usr/bin/swift sr_5076.swift 
["LD_LIBRARY_PATH": "/home/spse/swift-4.1-RELEASE-ubuntu16.04/usr/lib/swift/linux"]
["LD_LIBRARY_PATH": "/home/spse/swift-4.1-RELEASE-ubuntu16.04/usr/lib/swift/linux", "TEST": "FOOBAR"] 

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants