Skip to content
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

ttsc is not working on TS 4.9 #134

Closed
samchon opened this issue Nov 15, 2022 · 11 comments
Closed

ttsc is not working on TS 4.9 #134

samchon opened this issue Nov 15, 2022 · 11 comments

Comments

@samchon
Copy link
Contributor

samchon commented Nov 15, 2022

ts-node -C ttypescript works, but ttsc is not working.

code transformation does not working.

@Smrtnyk
Copy link

Smrtnyk commented Nov 23, 2022

It looks related to microsoft/TypeScript#51542
probably not an issue of ttypescript

@DanielRosenwasser
Copy link

4.9.4 is now out FWIW

@samchon
Copy link
Contributor Author

samchon commented Dec 8, 2022

TS has been updated to 4.9.4, but transformation is not working yet.

How about you @nonara ?

@samchon
Copy link
Contributor Author

samchon commented Dec 8, 2022

@DanielRosenwasser May I ask you one thing?

Looking at code of this ttypescript, it transforms TS source code by replacing ts.createProgram function. But after TS 4.9 patch, the transformed.createTransform function never be called. Is there any criticial change on TS not to call the createProgram function or overwrite the createProgram function variable when compiling?

export function patchCreateProgram(tsm: typeof ts, forceReadConfig = false, projectDir = process.cwd()) {
const originCreateProgram = tsm.createProgram as any;
function createProgram(createProgramOptions: ts.CreateProgramOptions): ts.Program;
function createProgram(
rootNames: ReadonlyArray<string>,
options: ts.CompilerOptions,
host?: ts.CompilerHost,
oldProgram?: ts.Program,
configFileParsingDiagnostics?: ReadonlyArray<ts.Diagnostic>
): ts.Program;
function createProgram(
rootNamesOrOptions: ReadonlyArray<string> | ts.CreateProgramOptions,
options?: ts.CompilerOptions,
host?: ts.CompilerHost,
oldProgram?: ts.Program,
configFileParsingDiagnostics?: ReadonlyArray<ts.Diagnostic>
): ts.Program {
let rootNames;
let createOpts: ts.CreateProgramOptions | undefined;
if (!Array.isArray(rootNamesOrOptions)) {
createOpts = rootNamesOrOptions as ts.CreateProgramOptions;
}
if (createOpts) {
rootNames = createOpts.rootNames;
options = createOpts.options;
host = createOpts.host;
oldProgram = createOpts.oldProgram;
configFileParsingDiagnostics = createOpts.configFileParsingDiagnostics;
} else {
options = options!;
rootNames = rootNamesOrOptions as ReadonlyArray<string>;
}
if (forceReadConfig) {
const info = getConfig(tsm, options, rootNames, projectDir);
options = info.compilerOptions;
if (createOpts) {
createOpts.options = options;
}
projectDir = info.projectDir;
}
const program: ts.Program = createOpts
? originCreateProgram(createOpts)
: originCreateProgram(rootNames, options, host, oldProgram, configFileParsingDiagnostics);
const plugins = preparePluginsFromCompilerOptions(options.plugins);
const pluginCreator = new PluginCreator(tsm, plugins, projectDir);
const originEmit = program.emit;
/**
* The emit method has the following declaration:
* https://github.com/microsoft/TypeScript/blob/bfc55b5762443c37ecdef08a3b5a4e057b4d1e85/src/compiler/builderPublic.ts#L101
* The declaration specifies 5 arguments, but it's not true. Sometimes the emit method takes 6 arguments.
*/
program.emit = function newEmit(
targetSourceFile?: ts.SourceFile,
writeFile?: ts.WriteFileCallback,
cancellationToken?: ts.CancellationToken,
emitOnlyDtsFiles?: boolean,
customTransformers?: ts.CustomTransformers,
arg?: boolean
): ts.EmitResult {
const mergedTransformers = pluginCreator.createTransformers({ program }, customTransformers);
const result: ts.EmitResult = (originEmit as any)(
targetSourceFile,
writeFile,
cancellationToken,
emitOnlyDtsFiles,
mergedTransformers,
arg
);
// todo: doesn't work with 3.7
// result.diagnostics = [...result.diagnostics, ...transformerErrors.get(program)!];
return result;
};
return program;
}
tsm.createProgram = createProgram;
return tsm;
}

@nonara
Copy link
Contributor

nonara commented Dec 8, 2022

@samchon I'll take a look tomorrow and work it out.

@samchon
Copy link
Contributor Author

samchon commented Dec 8, 2022

@cevek I just analyzed what makes ttypescript broken for a while. Hope my analysis to be helpful.


I'd tried hard coding special editing on tsc.js of TS and succeeded to transforming by modifing performCompilation function to be exported function. However, the performCompilation function is not exported member, but an internal function of executeCommandLine.ts file.

Therefore, to fix this bug occured after TS 4.9 update, ttypescript should update not only createProgram file, but also executeCommandLine function too.


@DanielRosenwasser Is it possible to change the executeCommandLine function provides an additional and optional prarameter that creating the Program class instance? Or can you re-patch the TS 4.9 (maybe 4.9.5) that only wrapping ts.createProgram can be working again? I think it would be really helpful for @cevek, developer of this ttypescript project and the others using this ttypescript library.

@nonara
Copy link
Contributor

nonara commented Dec 8, 2022

@samchon Libraries like tts and ts-patch augment the TypeScript libraries. It isn't the TS team's responsibility to modify the compiler to support our tools. The functions we modify are internal and not a part of public API, so it's on us to keep up with changes.

If this is an issue, I'll have ts-patch working later today. Unfortunately, I don't have access to modify this library, so you'll have to wait on @cevek to follow suit.

I will update in this thread when I've investigated the issue.

@nonara
Copy link
Contributor

nonara commented Dec 8, 2022

ts 4.9.4 support is added in ts-patch v2.1.0

samchon pushed a commit to samchon/ttypescript that referenced this issue Dec 9, 2022
@cevek cevek closed this as completed in f37f095 Dec 9, 2022
@forinda
Copy link

forinda commented Apr 2, 2023

ts 4.9.4 support is added in ts-patch v2.1.0

ts 4.9.4 support is added in ts-patch v2.1.0

So downgrading typescript from ^5.x.x to ^4.x.x works 🎉

@abbaty48
Copy link

No solution yet for ts5, downgrading to 4 works

@nonara
Copy link
Contributor

nonara commented Apr 20, 2023

Latest ts-patch rc (beta) supports ts v5, and I added some functionality so you don't have to do anything major to migrate from ttypescript. Just install and use tspc instead of ttsc

Here's more detail:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants