Skip to content

Commit

Permalink
fix: collect-dependends-symbol (#2079)
Browse files Browse the repository at this point in the history
<!-- Thank you for contributing! -->

### Description
1. Closed #2038
2. in main branch the testcase will panic.
```bash
thread '<unnamed>' panicked at crates/rolldown/src/types/symbols.rs:68:7:
canonical name not found for SymbolRef { owner: 3, symbol: SymbolId(0) }, original_name: "result"
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
```
<!-- Please insert your description here and provide especially info
about the "what" this PR is solving -->
  • Loading branch information
IWANABETHATGUY authored Aug 28, 2024
1 parent ea4965f commit 0205983
Show file tree
Hide file tree
Showing 9 changed files with 217 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,27 @@ impl<'a> GenerateStage<'a> {
symbol_needs_to_assign.push(*declared);
});

stmt_info.referenced_symbols.iter().for_each(|referenced| {
let referenced = referenced.symbol_ref();
let mut canonical_ref = symbols.par_canonical_ref_for(*referenced);
if let Some(namespace_alias) = &symbols.get(canonical_ref).namespace_alias {
canonical_ref = namespace_alias.namespace_ref;
}
depended_symbols.insert(canonical_ref);
stmt_info.referenced_symbols.iter().for_each(|reference_ref| {
match reference_ref {
rolldown_common::SymbolOrMemberExprRef::Symbol(referenced) => {
let mut canonical_ref = symbols.par_canonical_ref_for(*referenced);
if let Some(namespace_alias) = &symbols.get(canonical_ref).namespace_alias {
canonical_ref = namespace_alias.namespace_ref;
}
depended_symbols.insert(canonical_ref);
}
rolldown_common::SymbolOrMemberExprRef::MemberExpr(member_expr) => {
if let Some(sym_ref) = member_expr.resolved_symbol_ref(
&self.link_output.metas[module.idx].resolved_member_expr_refs,
) {
let canonical_ref = self.link_output.symbols.par_canonical_ref_for(sym_ref);
depended_symbols.insert(canonical_ref);
} else {
// `None` means the member expression resolve to a ambiguous export, which means it actually resolve to nothing.
// It would be rewrite to `undefined` in the final code, so we don't need to include anything to make `undefined` work.
}
}
};
});
});
});
Expand Down
1 change: 1 addition & 0 deletions crates/rolldown/tests/rolldown/issues/2038/_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
3 changes: 3 additions & 0 deletions crates/rolldown/tests/rolldown/issues/2038/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import * as d from './b.js'

export default d.result()
53 changes: 53 additions & 0 deletions crates/rolldown/tests/rolldown/issues/2038/artifacts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
source: crates/rolldown_testing/src/integration_test.rs
---
# Assets

## a.mjs

```js
import { result } from "./b.mjs";
//#region a.js
var a_default = result();
//#endregion
export { a_default as default };
```
## b.mjs

```js
//#region b.js
function result() {
return "result";
}
function test() {
return "test";
}
//#endregion
export { result, test };
```
## b~1.mjs

```js
import { result, test } from "./b.mjs";
export { result, test };
```
## main.mjs

```js
import { default as assert } from "node:assert";
//#region main.js
import("./b~1.mjs").then((mod) => {
assert.strictEqual(mod.result(), "result");
});
import("./a.mjs").then((mod) => {
assert.strictEqual(mod.default, "result");
});
//#endregion
```
7 changes: 7 additions & 0 deletions crates/rolldown/tests/rolldown/issues/2038/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function result() {
return 'result'
}

export function test() {
return 'test'
}
7 changes: 7 additions & 0 deletions crates/rolldown/tests/rolldown/issues/2038/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import assert from 'node:assert'
import("./b").then((mod) => {
assert.strictEqual(mod.result(), "result")
})
import("./a").then(mod => {
assert.strictEqual(mod.default, "result")
})
Original file line number Diff line number Diff line change
Expand Up @@ -1849,6 +1849,13 @@ expression: "snapshot_outputs.join(\"\\n\")"

- main-!~{000}~.mjs => main-40SKkhvQ.mjs

# tests/rolldown/issues/2038

- a-!~{005}~.mjs => a-un2hSNF7.mjs
- b-!~{001}~.mjs => b-gzBZQw6C.mjs
- b~1-!~{003}~.mjs => b~1-Yhc4kY0m.mjs
- main-!~{000}~.mjs => main-tyn51vg0.mjs

# tests/rolldown/issues/376

- main-!~{000}~.mjs => main-EXizxPDM.mjs
Expand Down
106 changes: 59 additions & 47 deletions packages/rolldown/src/rolldown-binding.wasi-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,53 +70,65 @@ function __napi_rs_initialize_modules(__napiInstance) {
__napiInstance.exports['__napi_register__transform_9']?.()
__napiInstance.exports['__napi_register__Bundler_struct_0']?.()
__napiInstance.exports['__napi_register__Bundler_impl_5']?.()
__napiInstance.exports['__napi_register__BindingInputItem_struct_6']?.()
__napiInstance.exports['__napi_register__BindingResolveOptions_struct_7']?.()
__napiInstance.exports['__napi_register__BindingTreeshake_struct_8']?.()
__napiInstance.exports['__napi_register__BindingInputOptions_struct_9']?.()
__napiInstance.exports['__napi_register__BindingOutputOptions_struct_10']?.()
__napiInstance.exports['__napi_register__BindingPluginContext_struct_11']?.()
__napiInstance.exports['__napi_register__BindingPluginContext_impl_17']?.()
__napiInstance.exports['__napi_register__BindingPluginContextResolvedId_struct_18']?.()
__napiInstance.exports['__napi_register__BindingPluginOptions_struct_19']?.()
__napiInstance.exports['__napi_register__BindingPluginWithIndex_struct_20']?.()
__napiInstance.exports['__napi_register__BindingTransformPluginContext_struct_21']?.()
__napiInstance.exports['__napi_register__BindingTransformPluginContext_impl_23']?.()
__napiInstance.exports['__napi_register__BindingAssetSource_struct_24']?.()
__napiInstance.exports['__napi_register__BindingEmittedAsset_struct_25']?.()
__napiInstance.exports['__napi_register__BindingHookLoadOutput_struct_26']?.()
__napiInstance.exports['__napi_register__BindingHookRenderChunkOutput_struct_27']?.()
__napiInstance.exports['__napi_register__BindingHookResolveIdExtraOptions_struct_28']?.()
__napiInstance.exports['__napi_register__BindingHookResolveIdOutput_struct_29']?.()
__napiInstance.exports['__napi_register__BindingHookSideEffects_30']?.()
__napiInstance.exports['__napi_register__BindingHookTransformOutput_struct_31']?.()
__napiInstance.exports['__napi_register__BindingPluginContextResolveOptions_struct_32']?.()
__napiInstance.exports['__napi_register__BindingBuiltinPlugin_struct_33']?.()
__napiInstance.exports['__napi_register__BindingBuiltinPluginName_34']?.()
__napiInstance.exports['__napi_register__BindingGlobImportPluginConfig_struct_35']?.()
__napiInstance.exports['__napi_register__BindingManifestPluginConfig_struct_36']?.()
__napiInstance.exports['__napi_register__BindingModulePreloadPolyfillPluginConfig_struct_37']?.()
__napiInstance.exports['__napi_register__ParallelJsPluginRegistry_struct_38']?.()
__napiInstance.exports['__napi_register__ParallelJsPluginRegistry_impl_40']?.()
__napiInstance.exports['__napi_register__register_plugins_41']?.()
__napiInstance.exports['__napi_register__BindingLog_struct_42']?.()
__napiInstance.exports['__napi_register__BindingLogLevel_43']?.()
__napiInstance.exports['__napi_register__BindingModuleInfo_struct_44']?.()
__napiInstance.exports['__napi_register__BindingModuleInfo_impl_46']?.()
__napiInstance.exports['__napi_register__BindingOutputAsset_struct_47']?.()
__napiInstance.exports['__napi_register__BindingOutputAsset_impl_52']?.()
__napiInstance.exports['__napi_register__BindingOutputChunk_struct_53']?.()
__napiInstance.exports['__napi_register__BindingOutputChunk_impl_71']?.()
__napiInstance.exports['__napi_register__BindingOutputs_struct_72']?.()
__napiInstance.exports['__napi_register__BindingOutputs_impl_76']?.()
__napiInstance.exports['__napi_register__FinalBindingOutputs_struct_77']?.()
__napiInstance.exports['__napi_register__FinalBindingOutputs_impl_80']?.()
__napiInstance.exports['__napi_register__PreRenderedChunk_struct_81']?.()
__napiInstance.exports['__napi_register__RenderedChunk_struct_82']?.()
__napiInstance.exports['__napi_register__BindingRenderedModule_struct_83']?.()
__napiInstance.exports['__napi_register__AliasItem_struct_84']?.()
__napiInstance.exports['__napi_register__BindingSourcemap_struct_85']?.()
__napiInstance.exports['__napi_register__BindingJsonSourcemap_struct_86']?.()
__napiInstance.exports['__napi_register__BindingInjectImportNamed_struct_6']?.()
__napiInstance.exports['__napi_register__BindingInjectImportNamespace_struct_7']?.()
__napiInstance.exports['__napi_register__BindingInputItem_struct_8']?.()
__napiInstance.exports['__napi_register__BindingResolveOptions_struct_9']?.()
__napiInstance.exports['__napi_register__BindingTreeshake_struct_10']?.()
__napiInstance.exports['__napi_register__BindingInputOptions_struct_11']?.()
__napiInstance.exports['__napi_register__BindingOutputOptions_struct_12']?.()
__napiInstance.exports['__napi_register__BindingPluginContext_struct_13']?.()
__napiInstance.exports['__napi_register__BindingPluginContext_impl_19']?.()
__napiInstance.exports['__napi_register__BindingPluginContextResolvedId_struct_20']?.()
__napiInstance.exports['__napi_register__BindingPluginOptions_struct_21']?.()
__napiInstance.exports['__napi_register__BindingPluginWithIndex_struct_22']?.()
__napiInstance.exports['__napi_register__BindingTransformPluginContext_struct_23']?.()
__napiInstance.exports['__napi_register__BindingTransformPluginContext_impl_25']?.()
__napiInstance.exports['__napi_register__BindingAssetSource_struct_26']?.()
__napiInstance.exports['__napi_register__BindingEmittedAsset_struct_27']?.()
__napiInstance.exports['__napi_register__BindingHookLoadOutput_struct_28']?.()
__napiInstance.exports['__napi_register__BindingHookRenderChunkOutput_struct_29']?.()
__napiInstance.exports['__napi_register__BindingHookResolveIdExtraArgs_struct_30']?.()
__napiInstance.exports['__napi_register__BindingHookResolveIdOutput_struct_31']?.()
__napiInstance.exports['__napi_register__BindingHookSideEffects_32']?.()
__napiInstance.exports['__napi_register__BindingHookTransformOutput_struct_33']?.()
__napiInstance.exports['__napi_register__BindingStringOrRegex_struct_34']?.()
__napiInstance.exports['__napi_register__BindingPluginContextResolveOptions_struct_35']?.()
__napiInstance.exports['__napi_register__BindingTransformHookExtraArgs_struct_36']?.()
__napiInstance.exports['__napi_register__BindingBuiltinPlugin_struct_37']?.()
__napiInstance.exports['__napi_register__BindingBuiltinPluginName_38']?.()
__napiInstance.exports['__napi_register__BindingGlobImportPluginConfig_struct_39']?.()
__napiInstance.exports['__napi_register__BindingManifestPluginConfig_struct_40']?.()
__napiInstance.exports['__napi_register__BindingModulePreloadPolyfillPluginConfig_struct_41']?.()
__napiInstance.exports['__napi_register__BindingJsonPluginConfig_struct_42']?.()
__napiInstance.exports['__napi_register__BindingTransformPluginConfig_struct_43']?.()
__napiInstance.exports['__napi_register__BindingAliasPluginConfig_struct_44']?.()
__napiInstance.exports['__napi_register__BindingAliasPluginAlias_struct_45']?.()
__napiInstance.exports['__napi_register__BindingBuildImportAnalysisPluginConfig_struct_46']?.()
__napiInstance.exports['__napi_register__BindingReplacePluginConfig_struct_47']?.()
__napiInstance.exports['__napi_register__BindingPluginOrder_48']?.()
__napiInstance.exports['__napi_register__BindingPluginHookMeta_struct_49']?.()
__napiInstance.exports['__napi_register__ParallelJsPluginRegistry_struct_50']?.()
__napiInstance.exports['__napi_register__ParallelJsPluginRegistry_impl_52']?.()
__napiInstance.exports['__napi_register__register_plugins_53']?.()
__napiInstance.exports['__napi_register__BindingLog_struct_54']?.()
__napiInstance.exports['__napi_register__BindingLogLevel_55']?.()
__napiInstance.exports['__napi_register__BindingModuleInfo_struct_56']?.()
__napiInstance.exports['__napi_register__BindingModuleInfo_impl_58']?.()
__napiInstance.exports['__napi_register__BindingOutputAsset_struct_59']?.()
__napiInstance.exports['__napi_register__BindingOutputAsset_impl_65']?.()
__napiInstance.exports['__napi_register__BindingOutputChunk_struct_66']?.()
__napiInstance.exports['__napi_register__BindingOutputChunk_impl_84']?.()
__napiInstance.exports['__napi_register__BindingOutputs_struct_85']?.()
__napiInstance.exports['__napi_register__BindingOutputs_impl_89']?.()
__napiInstance.exports['__napi_register__FinalBindingOutputs_struct_90']?.()
__napiInstance.exports['__napi_register__FinalBindingOutputs_impl_93']?.()
__napiInstance.exports['__napi_register__PreRenderedChunk_struct_94']?.()
__napiInstance.exports['__napi_register__RenderedChunk_struct_95']?.()
__napiInstance.exports['__napi_register__BindingRenderedModule_struct_96']?.()
__napiInstance.exports['__napi_register__AliasItem_struct_97']?.()
__napiInstance.exports['__napi_register__BindingSourcemap_struct_98']?.()
__napiInstance.exports['__napi_register__BindingJsonSourcemap_struct_99']?.()
}
export const BindingLog = __napiModule.exports.BindingLog
export const BindingModuleInfo = __napiModule.exports.BindingModuleInfo
Expand Down
Loading

0 comments on commit 0205983

Please sign in to comment.