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

feat(jsii): added a symbol identifier to every type in the assembly #3030

Merged
merged 13 commits into from
Oct 8, 2021

Conversation

otaviomacedo
Copy link
Contributor

Symbol identifiers follow the pattern <relative file path without extension>:<scope names>/<symbol name>

This will be useful for the generation of deprecation warnings and to improve the Rosetta samples.


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

… the assembly

This will be useful for the generation of deprecation warnings and to improve the Rosetta samples.
packages/@jsii/spec/lib/assembly.ts Outdated Show resolved Hide resolved
@otaviomacedo otaviomacedo changed the title feat: added a map from symbol identifiers to fully qualified names in the assembly feat: added a symbol identifier to every type in the assembly Oct 6, 2021
@otaviomacedo otaviomacedo requested a review from rix0rrr October 6, 2021 09:57
@@ -141,7 +142,8 @@
"primitive": "string"
}
}
]
],
"symbolId": "lib/index:BaseProps"
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the intention down the line to leverage the fact that this effectively contains a file path + declaration name?

If so, the relative path might be altered by tsc.outDir... And that might be worth considering?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For the deprecation warnings, the only requirement is that this string uniquely identifies the symbol. The idea is: the JSII assembly is the source of truth for deciding whether an element is deprecated. But when we traverse the AST, what we have are Typescript nodes. So this property is the bridge between the two domains. If we can deterministically generate the same strings when writing to the assembly and when reading from it, we can do this mapping.

@rix0rrr do you have other applications in mind?

Copy link
Contributor

@rix0rrr rix0rrr Oct 7, 2021

Choose a reason for hiding this comment

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

It's a good point, I had forgotten about that. The identifier should be stable regardless of whether we're looking at the "uncompiled" source, or the "compiled + distributed" sources that go up on NPM.

These are the potential cases we could be running into:

# Finding symbol in source tree (has original .ts files)
<src_repo>/<package>/src/file.ts

# Finding symbol in a packaged library without an outdir (will have .d.ts and .js files)
.../node_modules/<package>/src/file.d.ts

# Finding symbol in a packaged library WITH an outdir
.../node_modules/<package>/dist/src/file.d.ts

All should ideally have the same identifier, so that we can treat jsii packages that we consume via in-repo symlinks and via monorepo symlinks the same.

That kinda sucks actually :(.

Anyone any good ideas?

Copy link
Contributor

Choose a reason for hiding this comment

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

I mean... logically the identifier should have the path src/file in there. We have to think about how to extract that short relative path from this full path though:

.../node_modules/<package>/dist/src/file.d.ts

It requires us to be able to identify the <dist> dir in some way.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ho but that's actually not too hard. Given that we're looking for the package.json anyway, that will have outdir set under jsii.tscOptions (or whatever the configuration is again). So it would work something like this:

function determineRelativeFilename(fullPath: string) {
  const packageJsonDir = findPackageJson(fullPath);
  const pj = loadPackageJson(packageJsonDir);

  const root = pj.jsii?.tscOptions?.outdir ? path.join(packageJsonDir, pj.jsii?.tscOptions?.outdir) : packageJsonDir;

  return stripExtension(path.relative(root, fullPath));
}

@rix0rrr rix0rrr added the pr/do-not-merge This PR should not be merged at this time. label Oct 8, 2021
rix0rrr
rix0rrr previously approved these changes Oct 8, 2021
Copy link
Contributor

@rix0rrr rix0rrr left a comment

Choose a reason for hiding this comment

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

Tiny concern around path handling on Windows, otherwise love it!

}

function removePrefix(prefix: string, filePath: string) {
const prefixParts = prefix.split(path.sep);
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a bit dangerous. On Windows, the / will work fine (and so people might use it) but it's not in path.sep, and so the splitting will behave differently.

It's excellent that you thought of this, because it will be an issue. For safety, can we split on /\/|\\/g instead? (I know it looks daft but it's splitting on either \ or /).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. Thanks!

@otaviomacedo otaviomacedo removed the pr/do-not-merge This PR should not be merged at this time. label Oct 8, 2021
@mergify mergify bot dismissed rix0rrr’s stale review October 8, 2021 16:34

Pull request has been modified.

@mergify
Copy link
Contributor

mergify bot commented Oct 8, 2021

The title of this Pull Request does not conform with [Conventional Commits] guidelines. It will need to be adjusted before the PR can be merged.
[Conventional Commits]: https://www.conventionalcommits.org

@otaviomacedo otaviomacedo removed the request for review from RomainMuller October 8, 2021 16:44
@otaviomacedo otaviomacedo changed the title feat: added a symbol identifier to every type in the assembly feat(jsii): added a symbol identifier to every type in the assembly Oct 8, 2021
@otaviomacedo otaviomacedo requested a review from rix0rrr October 8, 2021 16:47
@mergify
Copy link
Contributor

mergify bot commented Oct 8, 2021

Thank you for contributing! ❤️ I will now look into making sure the PR is up-to-date, then proceed to try and merge it!

@mergify mergify bot added the pr/ready-to-merge This PR is ready to be merged. label Oct 8, 2021
@mergify
Copy link
Contributor

mergify bot commented Oct 8, 2021

Merging (with squash)...

@mergify
Copy link
Contributor

mergify bot commented Oct 8, 2021

Merging (with squash)...

@mergify mergify bot merged commit 2120d34 into main Oct 8, 2021
@mergify mergify bot deleted the otaviom/symbol-identifiers branch October 8, 2021 18:25
@mergify mergify bot removed the pr/ready-to-merge This PR is ready to be merged. label Oct 8, 2021
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.

3 participants