-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Use regular imports instead of require where possible #59017
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
import childProcess from "child_process"; | ||
import fs from "fs"; | ||
import net from "net"; | ||
import os from "os"; | ||
import readline from "readline"; | ||
import { | ||
CharacterCodes, | ||
combinePaths, | ||
|
@@ -9,7 +14,6 @@ import { | |
getDirectoryPath, | ||
getRootLength, | ||
LanguageServiceMode, | ||
MapLike, | ||
noop, | ||
noopFileWatcher, | ||
normalizePath, | ||
|
@@ -37,24 +41,6 @@ interface LogOptions { | |
logToFile?: boolean; | ||
} | ||
|
||
interface NodeChildProcess { | ||
send(message: any, sendHandle?: any): void; | ||
on(message: "message" | "exit", f: (m: any) => void): void; | ||
kill(): void; | ||
pid: number; | ||
} | ||
|
||
interface ReadLineOptions { | ||
input: NodeJS.ReadableStream; | ||
output?: NodeJS.WritableStream; | ||
terminal?: boolean; | ||
historySize?: number; | ||
} | ||
|
||
interface NodeSocket { | ||
write(data: string, encoding: string): boolean; | ||
} | ||
|
||
function parseLoggingEnvironmentString(logEnvStr: string | undefined): LogOptions { | ||
if (!logEnvStr) { | ||
return {}; | ||
|
@@ -123,41 +109,6 @@ function parseServerMode(): LanguageServiceMode | string | undefined { | |
/** @internal */ | ||
export function initializeNodeSystem(): StartInput { | ||
const sys = Debug.checkDefined(ts.sys) as ts.server.ServerHost; | ||
const childProcess: { | ||
execFileSync(file: string, args: string[], options: { stdio: "ignore"; env: MapLike<string>; }): string | Buffer; | ||
} = require("child_process"); | ||
|
||
interface Stats { | ||
isFile(): boolean; | ||
isDirectory(): boolean; | ||
isBlockDevice(): boolean; | ||
isCharacterDevice(): boolean; | ||
isSymbolicLink(): boolean; | ||
isFIFO(): boolean; | ||
isSocket(): boolean; | ||
dev: number; | ||
ino: number; | ||
mode: number; | ||
nlink: number; | ||
uid: number; | ||
gid: number; | ||
rdev: number; | ||
size: number; | ||
blksize: number; | ||
blocks: number; | ||
atime: Date; | ||
mtime: Date; | ||
ctime: Date; | ||
birthtime: Date; | ||
} | ||
|
||
const fs: { | ||
openSync(path: string, options: string): number; | ||
close(fd: number, callback: (err: NodeJS.ErrnoException) => void): void; | ||
writeSync(fd: number, buffer: Buffer, offset: number, length: number, position?: number): number; | ||
statSync(path: string): Stats; | ||
stat(path: string, callback?: (err: NodeJS.ErrnoException, stats: Stats) => any): void; | ||
} = require("fs"); | ||
|
||
class Logger implements Logger { | ||
private seq = 0; | ||
|
@@ -231,7 +182,7 @@ export function initializeNodeSystem(): StartInput { | |
if (this.fd >= 0) { | ||
const buf = Buffer.from(s); | ||
// eslint-disable-next-line no-restricted-syntax | ||
fs.writeSync(this.fd, buf, 0, buf.length, /*position*/ null!); // TODO: GH#18217 | ||
fs.writeSync(this.fd, buf, 0, buf.length, /*position*/ null); | ||
} | ||
if (this.traceToConsole) { | ||
console.warn(s); | ||
|
@@ -326,7 +277,7 @@ export function initializeNodeSystem(): StartInput { | |
|
||
let cancellationToken: ts.server.ServerCancellationToken; | ||
try { | ||
const factory = require("./cancellationToken"); | ||
const factory = require("./cancellationToken.js"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi, I came across this line running dependency-cruiser on the typescript codebase. After building, it's the only require/import that can't be resolved. I notice that this file, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file should exist as a sibling. https://unpkg.com/browse/typescript@5.6.3/lib/cancellationToken.js Are you running this tool on the built code, or the source? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (realistically, it's not clear to me why this is a separate file at all anyway) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh I see. I'm running it on the source, so I guess it's missing some part of the build process that compiles Would it not work as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, that would not work. Our input source structure has nothing to do with our output source structure; it's bundled and placed in a different directory. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it - thanks very much! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #60250 you've inspired me |
||
cancellationToken = factory(sys.args); | ||
} | ||
catch (e) { | ||
|
@@ -439,31 +390,14 @@ function parseEventPort(eventPortStr: string | undefined) { | |
return eventPort !== undefined && !isNaN(eventPort) ? eventPort : undefined; | ||
} | ||
function startNodeSession(options: StartSessionOptions, logger: ts.server.Logger, cancellationToken: ts.server.ServerCancellationToken) { | ||
const childProcess: { | ||
fork(modulePath: string, args: string[], options?: { execArgv: string[]; env?: MapLike<string>; }): NodeChildProcess; | ||
} = require("child_process"); | ||
|
||
const os: { | ||
homedir?(): string; | ||
tmpdir(): string; | ||
} = require("os"); | ||
|
||
const net: { | ||
connect(options: { port: number; }, onConnect?: () => void): NodeSocket; | ||
} = require("net"); | ||
|
||
const readline: { | ||
createInterface(options: ReadLineOptions): NodeJS.EventEmitter; | ||
} = require("readline"); | ||
|
||
const rl = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout, | ||
terminal: false, | ||
}); | ||
|
||
class NodeTypingsInstallerAdapter extends ts.server.TypingsInstallerAdapter { | ||
protected override installer!: NodeChildProcess; | ||
protected override installer!: childProcess.ChildProcess; | ||
// This number is essentially arbitrary. Processing more than one typings request | ||
// at a time makes sense, but having too many in the pipe results in a hang | ||
// (see https://github.com/nodejs/node/issues/7657). | ||
|
@@ -533,7 +467,7 @@ function startNodeSession(options: StartSessionOptions, logger: ts.server.Logger | |
|
||
const typingsInstaller = combinePaths(getDirectoryPath(sys.getExecutingFilePath()), "typingsInstaller.js"); | ||
this.installer = childProcess.fork(typingsInstaller, args, { execArgv }); | ||
this.installer.on("message", m => this.handleMessage(m)); | ||
this.installer.on("message", m => this.handleMessage(m as any)); | ||
|
||
// We have to schedule this event to the next tick | ||
// cause this fn will be called during | ||
|
@@ -550,7 +484,7 @@ function startNodeSession(options: StartSessionOptions, logger: ts.server.Logger | |
|
||
class IOSession extends ts.server.Session { | ||
private eventPort: number | undefined; | ||
private eventSocket: NodeSocket | undefined; | ||
private eventSocket: net.Socket | undefined; | ||
private socketEventQueue: { body: any; eventName: string; }[] | undefined; | ||
/** No longer needed if syntax target is es6 or above. Any access to "this" before initialized will be a runtime error. */ | ||
private constructed: boolean | undefined; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any risk now that these are unconditional imports?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope, this is the tsserver project which only runs in node.