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

How to load file repeatedly #472

Closed
stellarhoof opened this issue Dec 4, 2017 · 7 comments
Closed

How to load file repeatedly #472

stellarhoof opened this issue Dec 4, 2017 · 7 comments
Labels
research Needs design work, investigation, or prototyping. Implementation uncertain.

Comments

@stellarhoof
Copy link

I've got a foo.ts file I'm editing and I want to .load foo.ts in the repl. First time works fine but subsequent times compilation fails because of Duplicate function implementation. I thought it was a cache issue but it also happens with ts-node --no-cache. Any way to clear compilation cache?

@stelcheck
Copy link
Contributor

This happens because the function is already defined in the same context.

> .load a
function a() {}

{}
> function a() {}
Thrown:  Unable to compile TypeScript
[eval].ts (-1,10): Duplicate function implementation. (2393)
[eval].ts (1,10): Duplicate function implementation. (2393)
> delete a
Thrown:  Unable to compile TypeScript
[eval].ts (1,8): The operand of a delete operator must be a property reference. (2703)
> a  = undefined
Thrown:  Unable to compile TypeScript
[eval].ts (1,1): Cannot assign to 'a' because it is not a variable. (2539)

The REPL interface behaves the same way as a single ts file; the cache is a compile cache, and has nothing to do with what happens at runtime.

@stellarhoof
Copy link
Author

Hmmm yeah you're right. I just thought .load would be smarter to invalidate all declarations beforehand.

@blakeembrey
Copy link
Member

How does load work in node.js with constant variables? If that works, we can try and find a way around this. The other alternative (and related to a similar issue on the REPL) is to disable a set of warnings automatically when using the REPL.

@stelcheck
Copy link
Contributor

> .load a.js
const a = 1;
 
undefined
> const a = 1;
SyntaxError: Identifier 'a' has already been declared

> 

@stellarhoof
Copy link
Author

@stelcheck That is standard behavior yes. But the const redeclaration doesn't come from a .load. This is fine. What I'm looking for is a way for load to invalidate previous declarations when invoked. So in your example, since const a was already declared, it'd not error out, instead it'd overwrite the definition. But only on .load! I don't know if this is currently possible. Google has yielded nothing for me.

@stelcheck
Copy link
Contributor

Since under the hood we use Node.js' REPL library - which, itself, is used when you run the actual node REPL - behaves as it does, I am not sure there would even be a way to do that for TypeScript. I hardly see how we would be able to keep track of which files define what ourselves TBH. Keep in mind that .load loads the file's code in the current context; once loaded, I doubt we'd have a proper way to figure out which method was defined by which file. At the very least, it would seem like a rather daunting task.

Of course, if you have any suggestions on how this would be implemented, I would be happy to help.

@blakeembrey blakeembrey added the research Needs design work, investigation, or prototyping. Implementation uncertain. label Dec 14, 2017
@cspotcode
Copy link
Collaborator

Today, users can avoid this issue via ts-node --transpile-only or ts-node --ignore-diagnostics 2393. This will suppress the "duplicate declaration" diagnostic.

In both the standard nodejs REPL and in ts-node, .load is like dot-sourcing or copy-pasting the loaded file's contents into a JS file. If you .load the following file twice in a row:

function foo() {}
const a = 123;

You get a REPL session that internally looks like:

function foo() {}
const a = 123;
function foo() {}
const a = 123;

node will throw an error because the const is redeclared. ts-node will throw the same error, but will also throw the TS diagnostic about a redeclared function. I think suppressing this diagnostic automatically would be incorrect, since users expect ts-node to typecheck. (unless they pass --transpile-only)

If we added magic to our REPL that invalidates previous declarations, this wouldn't be consistent with node's own REPL. I think this would also be incorrect. Plus it'd be super-complicated.

I feel pretty confident saying we can't / shouldn't do either of these solutions.

Closing to whittle down the list of open issues. If I'm wrong, let me know and I'll re-open.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
research Needs design work, investigation, or prototyping. Implementation uncertain.
Projects
None yet
Development

No branches or pull requests

4 participants