Skip to content

fix: don't propagate GDK_BACKEND to subprocs#29606

Closed
codebytere wants to merge 3 commits intomainfrom
fix-lint
Closed

fix: don't propagate GDK_BACKEND to subprocs#29606
codebytere wants to merge 3 commits intomainfrom
fix-lint

Conversation

@codebytere
Copy link
Copy Markdown
Member

Description of Change

#28898

was created before the Chromium changes to absl::optional and so caused build failures when it was merged.

Checklist

Release Notes

Notes: none.

@codebytere codebytere added the fast-track 🚅 Indicates that this PR is intended to bypass the 24 hour rule. Needs approval from Releases label Jun 9, 2021
@codebytere codebytere requested a review from ckerr June 9, 2021 09:57
@codebytere codebytere added the semver/patch backwards-compatible bug fixes label Jun 9, 2021
@zcbenz
Copy link
Copy Markdown
Contributor

zcbenz commented Jun 9, 2021

The GDK_BACKEND test is failing.

@codebytere
Copy link
Copy Markdown
Member Author

@zcbenz i think that CircleCI may set that 🤔 what do you think we should do? i think this got missed the first time bc of my CircleCI login problem

@zcbenz
Copy link
Copy Markdown
Contributor

zcbenz commented Jun 9, 2021

Hmm the reliable way to do the test is to start a child app with controlled environment variables. An example is how LC_ALL is tested in chromium-spec.ts.

@codebytere codebytere changed the title chore: fix lint failure fix: don't propagate GDK_BACKEND to subprocs Jun 10, 2021
@codebytere codebytere removed the fast-track 🚅 Indicates that this PR is intended to bypass the 24 hour rule. Needs approval from Releases label Jun 10, 2021
@electron-cation electron-cation bot added the new-pr 🌱 PR opened recently label Jun 10, 2021

it('does not propagate GDK_BACKEND', async () => {
const appPath = path.join(fixturesPath, 'api', 'gdk-backend-check');
appProcess = ChildProcess.spawn(process.execPath, [appPath]);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The child process should have GDK_BACKEND explicitly cleared in its environment variables.

Also we can probably add a test to explicitly set the GDK_BACKEND and check the result.

@electron-cation electron-cation bot removed the new-pr 🌱 PR opened recently label Jun 10, 2021
Copy link
Copy Markdown
Member

@ckerr ckerr left a comment

Choose a reason for hiding this comment

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

Several comments in this review but most of them are nits. I've tested this patch on a Wayland system (hirsute) & it WfM.

options.environment["MM_NOTTTY"] = "1";

// If the user set a GDK_BACKEND value of their own, use that,
// otherwise unset it becuase Chromium is setting GDK_BACKEND
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: becuase

throw new Error(`Process exited with code "${code}" signal "${signal}" output "${output}" stderr "${stderr}"`);
}

output = output.replace(/(\r\n|\n|\r)/gm, '');
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit The global flag isn't needed here

const appPath = path.join(fixturesPath, 'api', 'gdk-backend-check');
appProcess = ChildProcess.spawn(process.execPath, [appPath], {
env: {
GDK_BACKEND: 'wayland'
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

(opinion, style) This test repeats this hardcoded string later in the test. If we make a local variable named GDK_BACKEND then the repetition would be avoided, plus this line could then be slightly tighter as env: { GDK_BACKEND }.

throw new Error(`Process exited with code "${code}" signal "${signal}" output "${output}" stderr "${stderr}"`);
}

output = output.replace(/(\r\n|\n|\r)/gm, '');
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit The global flag isn't needed here

output = output.replace(/(\r\n|\n|\r)/gm, '');

const backend = String(process.env.GDK_BACKEND);
expect(output).to.not.equal(backend);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this test will fail if run on a system where the parent process' GDK_BACKEND actually is 'wayland'?

Maybe we should inject nonsense values into the child instead of using x11/wayland in these tests to ensure there's not conflict with the system the tests are running on

}
});

it('does not propagate GDK_BACKEND', async () => {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

OK there are some nits in this PR but overall I think the PR is good & works. This right here is the main thing that need to get changed -- the production code in this PR changes the environment variables in XDGUtil(), right before calling base::LaucnhProcess to invoke xdg-open or xdg-mail or (some various distro-specific way of trashing a file). This is fine, I think it addresses the use case in #28436

But this test is just looking at an arbitrarily-spawned ChildProcess. XDGUtil() isn't invoked for that and so I don't think these tests ever reach the codepaths that we're changing.

I see at least three options:

(1) decouple the GDK_BACKEND environment clobbering from Electron's XDGUtil() command, use extract-method to move it somewhere else that is more accessible by our tests

(2) Leave the production code where it is and use xdg-open to test the environment. This requires more complexity & setup in the tests, but should work with the production code as-is.. Loosely speaking, xdg-open works by looking at the filetype of the file being opened and looks for that file suffix in a .desktop file somewhere in the $XDG_DATA_HOME folder. When it finds it, It tries to launch the that .desktop file's app with the specified file. So we could make use of this by creating a script that takes a filename as input and dumps its environment variables to that file. Then we invent a file suffix that's not used by anyone else and create a fake .desktop entry that invokes our env-dumper script to "handle" that fake filesuffix. Then we call XDGOpen() for that file, which does reach the codepath we want to test. It should kick off our env dumper script. Then we can inspect the dumped env to see if the test passed. Then we remove the fake .desktop file from XDG_DATA_HOME to ensure our tests don't pollute the account running the test. 💫

(3) Remove the tests altogether. They are not testing what they're supposed to, so they shouldn't be left as-is. And doing a real test for this is kind of a pain. 🙂

My preference would be to try (2) but I wouldn't block the PR on it.

Comment on lines +130 to +131
// will make sure that they get unset from the environment via
// AlterEnvironment().
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice job explaining clearly & succinctly that passing in an empty string will clobber the value altogether 👍

My first thought reading the code was "This isn't unsetting it; it's setting it to an empty string" so it's great explain (a) that's not the case and (b) that it's AlterEnvironment that strips out key/vals where val is an empty string

Co-authored-by: Charles Kerr <charles@charleskerr.com>
@nfp0
Copy link
Copy Markdown

nfp0 commented Oct 8, 2023

Has there been any progress on this?
This is now also affecting any app launched in the VSCode integrated terminal window.

@georgexu99 georgexu99 closed this Nov 26, 2025
@georgexu99
Copy link
Copy Markdown
Contributor

Closed due to inactivity

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

Labels

semver/patch backwards-compatible bug fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants