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

fix: error if root typing path is invalid #557

Merged
merged 3 commits into from Oct 20, 2020

Conversation

Weakky
Copy link
Member

@Weakky Weakky commented Oct 15, 2020

Closes #396

We unfortunately cannot add a runtime error if the type that's imported from the path does not exist without substantial work and/or performance overhead.

If we use the current regex approach, we might end up throwing in cases where the type is properly exported but the regex fails to see that.

The only approach would be to parse the actual file with the type-checker, which is way out of scope for this PR.

@codecov
Copy link

codecov bot commented Oct 15, 2020

Codecov Report

Merging #557 into develop will decrease coverage by 0.19%.
The diff coverage is 30.00%.

@@             Coverage Diff             @@
##           develop     #557      +/-   ##
===========================================
- Coverage    93.99%   93.80%   -0.20%     
===========================================
  Files           42       42              
  Lines         2414     2420       +6     
  Branches       500      501       +1     
===========================================
+ Hits          2269     2270       +1     
- Misses         145      150       +5     

Comment on lines +164 to +182
if (typeof rootType !== 'string') {
const rootTypePath = rootType.path

if (typeof rootTypePath !== 'string' || !path.isAbsolute(rootTypePath)) {
throw new Error(
`Expected an absolute path for the root typing path of the type ${typeName}, saw ${rootTypePath}`
)
}

if (!fs.existsSync(rootTypePath)) {
throw new Error(`Root typing path ${rootTypePath} of the type ${typeName} does not exist`)
}

const importPath = relativePathTo(rootType.path, outputPath)
.replace(/(\.d)?\.ts/, '')
.replace(/\\+/g, '/')

importMap[importPath] = importMap[importPath] || new Set()
importMap[importPath].add(val.alias ? `${val.name} as ${val.alias}` : val.name)
importMap[importPath].add(rootType.alias ? `${rootType.name} as ${rootType.alias}` : rootType.name)
Copy link
Member Author

@Weakky Weakky Oct 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is technically a breaking change as I'm now only accepting absolute file paths, to ensure the ability to check whether the file exists.

@tgriesser I'm not sure what was the use-case for enabling any kind of path. Path aliases maybe? Do you feel strongly about keeping any string allowed? In that case, I'll make the checks only in case the path is absolute.

The reason I made it this way is that we're already that strict when configuring a typegenAutoConfig source.

Copy link
Member

@jasonkuhrt jasonkuhrt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, let's wait a bit to see if @tgriesser has thoughts too.

@jasonkuhrt
Copy link
Member

@Weakky since there were no tests docs or comments to indicate otherwise I think we can proceed with this PR.

@jasonkuhrt jasonkuhrt merged commit f8d164a into develop Oct 20, 2020
@jasonkuhrt jasonkuhrt deleted the fix/invalid-root-typing-path branch October 20, 2020 15:06
@Weakky
Copy link
Member Author

Weakky commented Oct 20, 2020

@jasonkuhrt I was planning to mention the breaking change in the squashed commit

@jasonkuhrt
Copy link
Member

@Weakky I don't think we have to consider them breaking because it was never documented or tested for that use-case. But you're right that it would be nice to mention. Let's add some detail in the release notes when we cut stable?

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

Successfully merging this pull request may close these issues.

A wrong rootTyping.path does not report any error/warning
2 participants