Skip to content

Trim LD_LIBRARY_PATH on startup #1740

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions ci/build/code-server.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,22 @@

# More complicated than readlink -f or realpath to support macOS.
# See https://github.com/cdr/code-server/issues/1537
bin_dir() {
root_dir() {
# We read the symlink, which may be relative from $0.
dst="$(readlink "$0")"
# We cd into the $0 directory.
cd "$(dirname "$0")" || exit 1
# Now we can cd into the dst directory.
cd "$(dirname "$dst")" || exit 1
# Finally we use pwd -P to print the absolute path of the directory of $dst.
# Now we can cd into the directory above the dst directory which is the root
# of the release.
cd "$(dirname "$dst")/.." || exit 1
# Finally we use pwd -P to print the absolute path the root.
pwd -P || exit 1
}

BIN_DIR=$(bin_dir)
ROOT="$(root_dir)"
if [ "$(uname)" = "Linux" ]; then
export LD_LIBRARY_PATH="$BIN_DIR/../lib${LD_LIBRARY_PATH+:$LD_LIBRARY_PATH}"
export LD_LIBRARY_PATH="$ROOT/lib:$LD_LIBRARY_PATH"
elif [ "$(uname)" = "Darwin" ]; then
export DYLD_LIBRARY_PATH="$BIN_DIR/../lib${DYLD_LIBRARY_PATH+:$DYLD_LIBRARY_PATH}"
export DYLD_LIBRARY_PATH="$ROOT/lib:$DYLD_LIBRARY_PATH"
fi
exec "$BIN_DIR/../lib/node" "$BIN_DIR/.." "$@"
exec "$ROOT/lib/node" "$ROOT" "$@"
37 changes: 27 additions & 10 deletions src/node/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ try {
const version = pkg.version || "development"
const commit = pkg.commit || "development"

const main = async (cliArgs: Args): Promise<void> => {
const configArgs = await readConfigFile(cliArgs.config)
// This prioritizes the flags set in args over the ones in the config file.
let args = Object.assign(configArgs, cliArgs)

const main = async (args: Args, cliArgs: Args, configArgs: Args): Promise<void> => {
if (!args.auth) {
args = {
...args,
Expand Down Expand Up @@ -126,19 +122,40 @@ const main = async (cliArgs: Args): Promise<void> => {
}
}

function trimLDLibraryPath(): void {
let ldVar: string
if (process.platform === "linux") {
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 we might want to check if the variable exists or something rather than use the platform because process.platform returns non-linux values even if uname normally returns Linux. https://nodejs.org/api/process.html#process_process_platform

Copy link
Member

@code-asher code-asher May 28, 2020

Choose a reason for hiding this comment

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

Actually that might only be the case for Android. 🤔 The rest seem to match up in terms of uname vs process.platform.

ldVar = "LD_LIBRARY_PATH"
} else if (process.platform === "darwin") {
ldVar = "DYLD_LIBRARY_PATH"
} else {
return
}

// Removes the leading path added by ./ci/build/code-server.sh to use our bundled
// dynamic libraries. See ci/build/build-standalone-release.sh
// This is required to avoid child processes using our bundled libraries.
process.env[ldVar] = process.env[ldVar]?.replace(path.dirname(process.execPath) + ":", "")
Copy link
Contributor

Choose a reason for hiding this comment

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

I am not sure that LD_LIBRARY_PATH is populated at this point so it might be empty. You can confirm correct operation if you run the following from a child process:

ldd  $(which cmake) | grep code-server

if you see any output it is still pointing at code server then it might not have taken. I think you might need to do something like AppImage where they rewrite the linker path for the binary to their library paths, that way you will not pollute downstream. I am not sure if this https://medium.com/@nehckl0/creating-relocatable-linux-executables-by-setting-rpath-with-origin-45de573a2e98 is feasible. That might be the best hope since you can put that in the installer when the location is selected.

Copy link
Member

Choose a reason for hiding this comment

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

I built off this PR and can confirm there's no output for that command whereas there was before.

}

async function entry(): Promise<void> {
const tryParse = async (): Promise<Args> => {
trimLDLibraryPath()

const tryParse = async (): Promise<[Args, Args, Args]> => {
try {
let args = parse(process.argv.slice(2))
const cliArgs = parse(process.argv.slice(2))
const configArgs = await readConfigFile(cliArgs.config)
// This prioritizes the flags set in args over the ones in the config file.
let args = Object.assign(configArgs, cliArgs)
args = await setDefaults(args)
return args
return [args, cliArgs, configArgs]
} catch (error) {
console.error(error.message)
process.exit(1)
}
}

const args = await tryParse()
const [args, cliArgs, configArgs] = await tryParse()
if (args.help) {
console.log("code-server", version, commit)
console.log("")
Expand Down Expand Up @@ -182,7 +199,7 @@ async function entry(): Promise<void> {
})
vscode.on("exit", (code) => process.exit(code || 0))
} else {
wrap(() => main(args))
wrap(() => main(args, cliArgs, configArgs))
}
}

Expand Down