Skip to content

Commit

Permalink
fix: provide default watch host (#4284)
Browse files Browse the repository at this point in the history
  • Loading branch information
hacke2 authored Dec 30, 2024
1 parent 03b7ffb commit 5355b54
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 31 deletions.
27 changes: 4 additions & 23 deletions packages/extension/src/node/extension.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
isElectronNode,
isWindows,
} from '@opensumi/ide-core-node';
import { process as processUtil } from '@opensumi/ide-utils';

import {
CONNECTION_HANDLE_BETWEEN_EXTENSION_AND_MAIN_THREAD,
Expand All @@ -54,24 +55,6 @@ import { ExtensionScanner } from './extension.scanner';

import type cp from 'child_process';

function detectExtFileType(): 'js' | 'ts' {
if (typeof module === 'object' && module.filename) {
if (module.filename.endsWith('.ts')) {
return 'ts';
}
return 'js';
}

if (typeof __filename === 'string') {
if (__filename.endsWith('.ts')) {
return 'ts';
}
return 'js';
}

return 'js';
}

@Injectable()
export class ExtensionNodeServiceImpl implements IExtensionNodeService {
private LOG_TAG = 'ExtensionNodeServiceImpl:' + Date.now();
Expand Down Expand Up @@ -338,16 +321,14 @@ export class ExtensionNodeServiceImpl implements IExtensionNodeService {

forkArgs.push(`--${KT_PROCESS_SOCK_OPTION_KEY}=${JSON.stringify(extServerListenOption)}`);

const extFileType = detectExtFileType();

if (isElectronNode()) {
extProcessPath = this.appConfig.extHost || (process.env.EXTENSION_HOST_ENTRY as string);
} else {
preloadPath =
process.env.EXT_MODE === 'js'
? path.join(__dirname, '../../lib/hosted/ext.host.js')
: path.join(__dirname, '../hosted/ext.host.' + extFileType);
if (process.env.EXT_MODE !== 'js' && extFileType === 'ts') {
: path.join(__dirname, '../hosted/ext.host.' + processUtil.extFileType);
if (process.env.EXT_MODE !== 'js' && processUtil.extFileType === 'ts') {
forkOptions.execArgv = forkOptions.execArgv.concat(['-r', 'ts-node/register', '-r', 'tsconfig-paths/register']);
}

Expand All @@ -358,7 +339,7 @@ export class ExtensionNodeServiceImpl implements IExtensionNodeService {
extProcessPath =
process.env.EXT_MODE === 'js'
? path.join(__dirname, '../../hosted/ext.process.js')
: path.join(__dirname, '../hosted/ext.process.' + extFileType);
: path.join(__dirname, '../hosted/ext.process.' + processUtil.extFileType);
}
}
this.logger.log(`Extension host process path ${extProcessPath}`);
Expand Down
22 changes: 14 additions & 8 deletions packages/file-service/src/node/watcher-process-manager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ChildProcess, fork } from 'child_process';
import { Server, Socket, createServer } from 'net';
import path from 'path';

import { Autowired, Injectable } from '@opensumi/di';
import { IRPCProtocol } from '@opensumi/ide-connection';
Expand All @@ -9,6 +10,7 @@ import { ILogServiceManager, SupportLogNamespace } from '@opensumi/ide-core-comm
import { DidFilesChangedParams, FileSystemWatcherClient } from '@opensumi/ide-core-common/lib/types/file-watch';
import { normalizedIpcHandlerPathAsync } from '@opensumi/ide-core-common/lib/utils/ipc';
import { AppConfig, Deferred, ILogService, UriComponents } from '@opensumi/ide-core-node';
import { process as processUtil } from '@opensumi/ide-utils';

import {
IWatcherHostService,
Expand Down Expand Up @@ -99,6 +101,15 @@ export class WatcherProcessManagerImpl implements IWatcherProcessManager {
});
}

get watcherHost() {
return (
this.appConfig.watcherHost ||
(process.env.EXT_MODE === 'js'
? path.join(__dirname, '../../lib/node/hosted/watcher.process.js')
: path.join(__dirname, 'hosted', 'watcher.process.' + processUtil.extFileType))
);
}

private async createWatcherProcess(clientId: string, ipcHandlerPath: string) {
const forkArgs = [
`--${SUMI_WATCHER_PROCESS_SOCK_KEY}=${JSON.stringify({
Expand All @@ -111,8 +122,8 @@ export class WatcherProcessManagerImpl implements IWatcherProcessManager {
})}`,
];

this.logger.log('Watcher process path: ', this.appConfig.watcherHost);
this.watcherProcess = fork(this.appConfig.watcherHost!, forkArgs, {
this.logger.log('Watcher process path: ', this.watcherHost);
this.watcherProcess = fork(this.watcherHost, forkArgs, {
silent: true,
});

Expand All @@ -126,13 +137,8 @@ export class WatcherProcessManagerImpl implements IWatcherProcessManager {
}

async createProcess(clientId: string) {
if (!this.appConfig.watcherHost) {
this.logger.error('watcherHost is not set');
return;
}

this.logger.log('create watcher process for client: ', clientId);
this.logger.log('appconfig watcherHost: ', this.appConfig.watcherHost);
this.logger.log('appconfig watcherHost: ', this.watcherHost);

const ipcHandlerPath = await this.getIPCHandlerPath('watcher_process');
await this.createWatcherServer(clientId, ipcHandlerPath);
Expand Down
19 changes: 19 additions & 0 deletions packages/utils/src/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,26 @@ if (typeof process === 'undefined') {
}
}

function detectExtFileType(): 'js' | 'ts' {
if (typeof module === 'object' && module.filename) {
if (module.filename.endsWith('.ts')) {
return 'ts';
}
return 'js';
}

if (typeof __filename === 'string') {
if (__filename.endsWith('.ts')) {
return 'ts';
}
return 'js';
}

return 'js';
}

export const cwd = safeProcess.cwd;
export const env = safeProcess.env;
export const platform = safeProcess.platform;
export const nextTick = safeProcess.nextTick;
export const extFileType = detectExtFileType();

0 comments on commit 5355b54

Please sign in to comment.