fix: importing from electron/utility in ESM (#47998)

This commit is contained in:
David Sanders
2025-08-09 00:47:47 -07:00
committed by GitHub
parent a201d6c541
commit d6c0691a63
22 changed files with 228 additions and 7 deletions

View File

@@ -256,6 +256,41 @@ describe('utilityProcess module', () => {
await once(child, 'exit');
expect(log).to.equal(pathToFileURL(fixtureFile) + '\n');
});
it('import \'electron/lol\' should throw', async () => {
const child = utilityProcess.fork(path.join(fixturesPath, 'electron-modules', 'import-lol.mjs'), [], {
stdio: ['ignore', 'ignore', 'pipe']
});
let stderr = '';
child.stderr!.on('data', (data) => { stderr += data.toString('utf8'); });
const [code] = await once(child, 'exit');
expect(code).to.equal(1);
expect(stderr).to.match(/Error \[ERR_MODULE_NOT_FOUND\]/);
});
it('import \'electron/main\' should not throw', async () => {
const child = utilityProcess.fork(path.join(fixturesPath, 'electron-modules', 'import-main.mjs'));
const [code] = await once(child, 'exit');
expect(code).to.equal(0);
});
it('import \'electron/renderer\' should not throw', async () => {
const child = utilityProcess.fork(path.join(fixturesPath, 'electron-modules', 'import-renderer.mjs'));
const [code] = await once(child, 'exit');
expect(code).to.equal(0);
});
it('import \'electron/common\' should not throw', async () => {
const child = utilityProcess.fork(path.join(fixturesPath, 'electron-modules', 'import-common.mjs'));
const [code] = await once(child, 'exit');
expect(code).to.equal(0);
});
it('import \'electron/utility\' should not throw', async () => {
const child = utilityProcess.fork(path.join(fixturesPath, 'electron-modules', 'import-utility.mjs'));
const [code] = await once(child, 'exit');
expect(code).to.equal(0);
});
});
describe('pid property', () => {

View File

@@ -65,6 +65,32 @@ describe('esm', () => {
expect(result.code).to.equal(0);
expect(result.stdout).to.equal('Exit with app, ready: false');
});
it('import \'electron/lol\' should throw', async () => {
const result = await runFixture(path.resolve(fixturePath, 'electron-modules', 'import-lol.mjs'));
expect(result.code).to.equal(1);
expect(result.stderr).to.match(/Error \[ERR_MODULE_NOT_FOUND\]/);
});
it('import \'electron/main\' should not throw', async () => {
const result = await runFixture(path.resolve(fixturePath, 'electron-modules', 'import-main.mjs'));
expect(result.code).to.equal(0);
});
it('import \'electron/renderer\' should not throw', async () => {
const result = await runFixture(path.resolve(fixturePath, 'electron-modules', 'import-renderer.mjs'));
expect(result.code).to.equal(0);
});
it('import \'electron/common\' should not throw', async () => {
const result = await runFixture(path.resolve(fixturePath, 'electron-modules', 'import-common.mjs'));
expect(result.code).to.equal(0);
});
it('import \'electron/utility\' should not throw', async () => {
const result = await runFixture(path.resolve(fixturePath, 'electron-modules', 'import-utility.mjs'));
expect(result.code).to.equal(0);
});
});
describe('renderer process', () => {
@@ -213,5 +239,43 @@ describe('esm', () => {
});
});
});
describe('electron modules', () => {
it('import \'electron/lol\' should throw', async () => {
const [, error] = await loadWindowWithPreload('import { ipcRenderer } from "electron/lol";', {
sandbox: false
});
expect(error).to.not.equal(null);
expect(error?.message).to.match(/Cannot find package 'electron'/);
});
it('import \'electron/main\' should not throw', async () => {
const [, error] = await loadWindowWithPreload('import { ipcRenderer } from "electron/main";', {
sandbox: false
});
expect(error).to.equal(null);
});
it('import \'electron/renderer\' should not throw', async () => {
const [, error] = await loadWindowWithPreload('import { ipcRenderer } from "electron/renderer";', {
sandbox: false
});
expect(error).to.equal(null);
});
it('import \'electron/common\' should not throw', async () => {
const [, error] = await loadWindowWithPreload('import { ipcRenderer } from "electron/common";', {
sandbox: false
});
expect(error).to.equal(null);
});
it('import \'electron/utility\' should not throw', async () => {
const [, error] = await loadWindowWithPreload('import { ipcRenderer } from "electron/utility";', {
sandbox: false
});
expect(error).to.equal(null);
});
});
});
});

View File

@@ -0,0 +1,3 @@
import { net } from 'electron/common';
process.exit(net !== undefined ? 0 : 1);

View File

@@ -0,0 +1,3 @@
import { net } from 'electron/lol';
process.exit(net !== undefined ? 0 : 1);

View File

@@ -0,0 +1,3 @@
import { net } from 'electron/main';
process.exit(net !== undefined ? 0 : 1);

View File

@@ -0,0 +1,3 @@
import { net } from 'electron/renderer';
process.exit(net !== undefined ? 0 : 1);

View File

@@ -0,0 +1,3 @@
import { net } from 'electron/utility';
process.exit(net !== undefined ? 0 : 1);

View File

@@ -0,0 +1,3 @@
const { net } = require('electron/common');
process.exit(net !== undefined ? 0 : 1);

View File

@@ -0,0 +1,3 @@
const { net } = require('electron/lol');
process.exit(net !== undefined ? 0 : 1);

View File

@@ -0,0 +1,3 @@
const { net } = require('electron/main');
process.exit(net !== undefined ? 0 : 1);

View File

@@ -0,0 +1,3 @@
const { net } = require('electron/renderer');
process.exit(net !== undefined ? 0 : 1);

View File

@@ -0,0 +1,3 @@
const { net } = require('electron/utility');
process.exit(net !== undefined ? 0 : 1);

View File

@@ -0,0 +1,8 @@
process.on('uncaughtException', (err) => {
console.error(err);
process.exit(1);
});
const { net } = await import('electron/common');
process.exit(net !== undefined ? 0 : 1);

View File

@@ -0,0 +1,8 @@
process.on('uncaughtException', (err) => {
console.error(err);
process.exit(1);
});
const { net } = await import('electron/lol');
process.exit(net !== undefined ? 0 : 1);

View File

@@ -0,0 +1,8 @@
process.on('uncaughtException', (err) => {
console.error(err);
process.exit(1);
});
const { net } = await import('electron/main');
process.exit(net !== undefined ? 0 : 1);

View File

@@ -0,0 +1,8 @@
process.on('uncaughtException', (err) => {
console.error(err);
process.exit(1);
});
const { net } = await import('electron/renderer');
process.exit(net !== undefined ? 0 : 1);

View File

@@ -0,0 +1,8 @@
process.on('uncaughtException', (err) => {
console.error(err);
process.exit(1);
});
const { net } = await import('electron/utility');
process.exit(net !== undefined ? 0 : 1);

View File

@@ -1,4 +1,4 @@
import { BrowserWindow } from 'electron/main';
import { BrowserWindow, utilityProcess } from 'electron/main';
import { expect } from 'chai';
@@ -82,6 +82,8 @@ describe('modules support', () => {
});
describe('require(\'electron/...\')', () => {
const utilityProcessFixturesPath = path.resolve(__dirname, 'fixtures', 'api', 'utility-process', 'electron-modules');
it('require(\'electron/lol\') should throw in the main process', () => {
expect(() => {
require('electron/lol');
@@ -94,6 +96,17 @@ describe('modules support', () => {
await expect(w.webContents.executeJavaScript('{ require(\'electron/lol\'); null }')).to.eventually.be.rejected();
});
it('require(\'electron/lol\') should throw in the utility process', async () => {
const child = utilityProcess.fork(path.join(utilityProcessFixturesPath, 'require-lol.js'), [], {
stdio: ['ignore', 'ignore', 'pipe']
});
let stderr = '';
child.stderr!.on('data', (data) => { stderr += data.toString('utf8'); });
const [code] = await once(child, 'exit');
expect(code).to.equal(1);
expect(stderr).to.match(/Cannot find module 'electron\/lol'/);
});
it('require(\'electron\') should not throw in the main process', () => {
expect(() => {
require('electron');
@@ -118,6 +131,12 @@ describe('modules support', () => {
await expect(w.webContents.executeJavaScript('{ require(\'electron/main\'); null }')).to.be.fulfilled();
});
it('require(\'electron/main\') should not throw in the utility process', async () => {
const child = utilityProcess.fork(path.join(utilityProcessFixturesPath, 'require-main.js'));
const [code] = await once(child, 'exit');
expect(code).to.equal(0);
});
it('require(\'electron/renderer\') should not throw in the main process', () => {
expect(() => {
require('electron/renderer');
@@ -130,6 +149,12 @@ describe('modules support', () => {
await expect(w.webContents.executeJavaScript('{ require(\'electron/renderer\'); null }')).to.be.fulfilled();
});
it('require(\'electron/renderer\') should not throw in the utility process', async () => {
const child = utilityProcess.fork(path.join(utilityProcessFixturesPath, 'require-renderer.js'));
const [code] = await once(child, 'exit');
expect(code).to.equal(0);
});
it('require(\'electron/common\') should not throw in the main process', () => {
expect(() => {
require('electron/common');
@@ -141,6 +166,30 @@ describe('modules support', () => {
w.loadURL('about:blank');
await expect(w.webContents.executeJavaScript('{ require(\'electron/common\'); null }')).to.be.fulfilled();
});
it('require(\'electron/common\') should not throw in the utility process', async () => {
const child = utilityProcess.fork(path.join(utilityProcessFixturesPath, 'require-common.js'));
const [code] = await once(child, 'exit');
expect(code).to.equal(0);
});
it('require(\'electron/utility\') should not throw in the main process', () => {
expect(() => {
require('electron/utility');
}).to.not.throw();
});
it('require(\'electron/utility\') should not throw in the renderer process', async () => {
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
w.loadURL('about:blank');
await expect(w.webContents.executeJavaScript('{ require(\'electron/utility\'); null }')).to.be.fulfilled();
});
it('require(\'electron/utility\') should not throw in the utility process', async () => {
const child = utilityProcess.fork(path.join(utilityProcessFixturesPath, 'require-utility.js'));
const [code] = await once(child, 'exit');
expect(code).to.equal(0);
});
});
describe('coffeescript', () => {