Skip to content

Commit

Permalink
Merge pull request #58 from neon-bindings/neon-dist-normalize-crate-n…
Browse files Browse the repository at this point in the history
…ames

fix(cli): Proper crate name normalization
  • Loading branch information
dherman authored May 18, 2024
2 parents b6ed2fc + 3dfb309 commit b8c3343
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@ jobs:
shell: bash
run: |
echo "***** github.event:"
echo '${{ toJSON(github.event) }}'
cat <<GITHUB_EVENT_JSON
${{toJSON(github.event)}}
GITHUB_EVENT_JSON
echo "***** github.repository:"
echo '${{ toJSON(github.repository) }}'
cat <<GITHUB_REPOSITORY_JSON
${{toJSON(github.repository)}}
GITHUB_REPOSITORY_JSON
echo "***** github.head_ref:"
echo 'ref: ${{ github.event_name == 'issue_comment' && github.head_ref }}'
echo "***** github.event.repository.default_branch:"
Expand Down
18 changes: 11 additions & 7 deletions pkgs/cargo-messages/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,15 @@ enum CargoMessages {
FromFile(MessageIter<BufReader<File>>, Option<MountInfo>, bool)
}

// Starting around Rust 1.78 or 1.79, cargo will begin normalizing
// crate names in the JSON output, so to support both old and new
// versions of cargo, we need to compare against both variants.
//
// See: https://github.com/rust-lang/cargo/issues/13867
fn normalize(crate_name: &str) -> String {
crate_name.replace('-', "_")
}

impl CargoMessages {
fn verbose(&self) -> bool {
match self {
Expand Down Expand Up @@ -188,12 +197,7 @@ impl CargoMessages {
let mut count: u32 = 0;
let mut result: Option<Artifact> = None;

// Starting around Rust 1.78 or 1.79, cargo will begin normalizing
// crate names in the JSON output, so to support both old and new
// versions of cargo, we need to compare against both variants.
//
// See: https://github.com/rust-lang/cargo/issues/13867
let normalized_crate_name = &crate_name.replace('-', "_");
let normalized_crate_name = normalize(crate_name);

while let Some(msg) = self.next() {
match msg {
Expand All @@ -202,7 +206,7 @@ impl CargoMessages {
if self.verbose() {
eprintln!("[cargo-messages] found artifact for {}", artifact.target.name);
}
if result.is_none() && &artifact.target.name == crate_name || &artifact.target.name == normalized_crate_name {
if result.is_none() && normalize(&artifact.target.name) == normalized_crate_name {
result = Some(artifact);
}
}
Expand Down
13 changes: 12 additions & 1 deletion src/cli/src/commands/dist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ import { CargoReader } from 'cargo-messages';
import { LibraryManifest } from '@neon-rs/manifest';
import { assertIsNodePlatform } from '@neon-rs/manifest/platform';

// Starting around Rust 1.78 or 1.79, cargo will begin normalizing
// crate names in the JSON output, so to support both old and new
// versions of cargo, we need to compare against both variants.
//
// See: https://github.com/rust-lang/cargo/issues/13867
function normalize(crateName: string): string {
return crateName.replaceAll(/-/g, "_");
}

// FIXME: add options to infer crate name from manifests
// --package <path/to/package.json>
// --crate <path/to/Cargo.toml>
Expand Down Expand Up @@ -104,6 +113,7 @@ export default class Dist implements Command {
private _mount: string | null;
private _manifestPath: string | null;
private _crateName: string;
private _normalizedCrateName: string;
private _out: Promise<OutputFileParse>;
private _verbose: boolean;

Expand All @@ -128,6 +138,7 @@ export default class Dist implements Command {
this._manifestPath = options['manifest-path'];
this._crateName = options.name ||
basename(ensureDefined(process.env['npm_package_name'], '$npm_package_name'));
this._normalizedCrateName = normalize(this._crateName);
this._out = parseOutputFile(options.debug, options.out, options.platform);
this._verbose = !!options.verbose;

Expand All @@ -144,7 +155,7 @@ export default class Dist implements Command {
let file: string | null = null;

for await (const msg of reader) {
if (!file && msg.isCompilerArtifact() && msg.crateName() === this._crateName) {
if (!file && msg.isCompilerArtifact() && normalize(msg.crateName()) === this._normalizedCrateName) {
file = msg.findFileByCrateType('cdylib');
}
}
Expand Down

0 comments on commit b8c3343

Please sign in to comment.