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

fix Issue 18729 - dmd -run executes in different environment #8121

Merged
merged 1 commit into from
Aug 6, 2019

Conversation

marler8997
Copy link
Contributor

@marler8997 marler8997 commented Apr 3, 2018

TLDR: the following fails on windows

echo void main(){} > voidmain.d
dmd -run rdmd.d -m64 voidmain.d

OPTLINK (R) for Win32  Release 8.00.17
Copyright (C) Digital Mars 1989-2013  All rights reserved.
http://www.digitalmars.com/ctg/optlink.html
OPTLINK : Warning 9: Unknown Option : OUT
OPTLINK : Warning 9: Unknown Option : LIBPATH
OPTLINK : Warning 9: Unknown Option : LIBPATH
OPTLINK : Warning 9: Unknown Option : LIBPATH
...

I ran into this problem when I was running a D program via dmd -run that itself invoked DMD. The resulting program was invoking the compiler with -m64, but the original dmd -run did not use -m64. This caused a problem because the first invocation of the compiler set the LINKCMD environment variable to the OPTLINK executable, which caused the second invocation to use the OPTLINK executable but it treated it like the MSVC linker because it was running in 64-bit mode.

The solution here is to maintain the original environment (the environment before it was changed by the compiler) when dmd -run executes the compiled program. This PR accomplishes this by saving the original values of environment variables when they are overwritten and then restoring these values before executing the compiled program.

@dlang-bot
Copy link
Contributor

dlang-bot commented Apr 3, 2018

Thanks for your pull request and interest in making D better, @marler8997! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Auto-close Bugzilla Severity Description
18729 normal dmd -run executes in different environment

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub fetch digger
dub run digger -- build "master + dmd#8121"

test/runnable/sameenv.sh Outdated Show resolved Hide resolved
@marler8997 marler8997 force-pushed the sameEnv branch 2 times, most recently from 2e6a314 to bbbd94f Compare April 3, 2018 23:51
src/dmd/link.d Outdated Show resolved Hide resolved
src/dmd/link.d Outdated Show resolved Hide resolved
src/dmd/mars.d Outdated Show resolved Hide resolved
src/dmd/link.d Outdated Show resolved Hide resolved
src/dmd/link.d Outdated Show resolved Hide resolved
src/dmd/link.d Outdated Show resolved Hide resolved
src/dmd/env.d Outdated Show resolved Hide resolved
src/dmd/env.d Outdated Show resolved Hide resolved
src/dmd/env.d Outdated Show resolved Hide resolved
src/dmd/env.d Outdated Show resolved Hide resolved
src/dmd/env.d Outdated Show resolved Hide resolved
src/dmd/env.d Outdated Show resolved Hide resolved
src/dmd/env.d Outdated Show resolved Hide resolved
src/dmd/env.d Outdated Show resolved Hide resolved
src/dmd/env.d Outdated Show resolved Hide resolved
src/dmd/env.d Outdated Show resolved Hide resolved
src/dmd/env.d Outdated Show resolved Hide resolved
src/dmd/env.d Outdated
return; // already saved
}
originalVars.push(LocalEnvVar.xmalloc(name,
name.toCStringThen!(n => getenv(n.ptr)).toDString));
Copy link
Member

Choose a reason for hiding this comment

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

There seems to be a lot of converting char* to [] and back again. Wouldn't it be simpler to just leave them as char*?

Copy link
Contributor Author

@marler8997 marler8997 Aug 5, 2019

Choose a reason for hiding this comment

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

There seems to be a lot of converting char* to [] and back again

Alot? This is the one and only place in this change that we're calling toCStringThen. And we're not converting name back to a DString, we're converting the return value of getenv to a DString.

We're using the char[] version of name for both the comparison and to pass to the LocalEnvVar allocation function. We only need the char* version to call getenv. I'm not sure what you'd like me to change here.

@marler8997
Copy link
Contributor Author

@WalterBright I've integrated all your suggestions except the one about toCStringThen since I'm not sure what you want for that suggestion. Please let me know what you'd like me to do next here.

@marler8997 marler8997 force-pushed the sameEnv branch 2 times, most recently from d20cc93 to 3d85f04 Compare August 5, 2019 15:13
@WalterBright
Copy link
Member

Thank you. In thinking about it some more, what's happening is simply storing name=value pairs, and avoiding making duplicate entries for name. This is exactly what D associative arrays do. You can dispense with the new structs, and just use:

__gshared string[string] envNameValues;

Use the in operator to test for membership, and foreach to iterate over envNameValues.

@marler8997 marler8997 force-pushed the sameEnv branch 3 times, most recently from 47b0b0e to 9a6975b Compare August 5, 2019 21:57
@marler8997
Copy link
Contributor Author

Ok, I've changed the code per your suggestions.

@WalterBright
Copy link
Member

Much better.

@WalterBright WalterBright merged commit b505b86 into dlang:master Aug 6, 2019
@marler8997
Copy link
Contributor Author

marler8997 commented Aug 6, 2019

Thanks alot for guiding this through to the finish line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants