Skip to content

Commit

Permalink
filter out CompileError::UnknownVariable errors when CONTRACT_ID
Browse files Browse the repository at this point in the history
…isn't injected into namespace (FuelLabs#4727)

## Description
When `CONTRACT_ID` is used in a test, the compiler returns
`CompileError::UnknownVariable` to LSP as `CONTRACT_ID` is only injected
into the namespace during building and not checking. As such, we are
filtering out these warnings in the language server so they do not
appear to the end user.

closes FuelLabs/sway-vscode-plugin#154
  • Loading branch information
JoshuaBatty authored Jul 5, 2023
1 parent 594e09a commit 91a795d
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions forc-pkg/src/pkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2586,16 +2586,29 @@ pub fn check(
let compiled_contract_deps = HashMap::new();

let mut results = vec![];
for &node in plan.compilation_order.iter() {
for (idx, &node) in plan.compilation_order.iter().enumerate() {
let pkg = &plan.graph[node];
let manifest = &plan.manifest_map()[&pkg.id()];

// This is necessary because `CONTRACT_ID` is a special constant that's injected into the
// compiler's namespace. Although we only know the contract id during building, we are
// inserting a dummy value here to avoid false error signals being reported in LSP.
// We only do this for the last node in the compilation order because previous nodes
// are dependencies.
//
// See this github issue for more context: https://github.com/FuelLabs/sway-vscode-plugin/issues/154
const DUMMY_CONTRACT_ID: &str =
"0x0000000000000000000000000000000000000000000000000000000000000000";
let contract_id_value =
(idx == plan.compilation_order.len() - 1).then(|| DUMMY_CONTRACT_ID.to_string());

let dep_namespace = dependency_namespace(
&lib_namespace_map,
&compiled_contract_deps,
&plan.graph,
node,
engines,
None,
contract_id_value,
)
.expect("failed to create dependency namespace");

Expand Down

0 comments on commit 91a795d

Please sign in to comment.