Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions internal/cadence/lint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,28 @@ func Test_Lint(t *testing.T) {
)
})

t.Run("resolves stdlib imports Crypto", func(t *testing.T) {
t.Parallel()

state := setupMockState(t)

results, err := lintFiles(state, "StdlibImportsCrypto.cdc")
require.NoError(t, err)

require.Equal(t,
&lintResult{
Results: []fileResult{
{
FilePath: "StdlibImportsCrypto.cdc",
Diagnostics: []analysis.Diagnostic{},
},
},
exitCode: 0,
},
results,
)
})

t.Run("resolves nested imports when contract imported by name", func(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -392,6 +414,15 @@ func setupMockState(t *testing.T) *flowkit.State {
let foo = getAuthAccount<&Account>(0x01)
log(RLP.getType())
}`), 0644)
_ = afero.WriteFile(mockFs, "StdlibImportsCrypto.cdc", []byte(`
import Crypto

access(all) contract CryptoImportTest {
access(all) fun test(): Void {
let _ = Crypto.hash([1, 2, 3], algorithm: HashAlgorithm.SHA3_256)
Copy link

Copilot AI Dec 29, 2025

Choose a reason for hiding this comment

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

The discarded variable uses the blank identifier _, but for test clarity, consider using a descriptive name like hashResult to make the test's intent more explicit.

Suggested change
let _ = Crypto.hash([1, 2, 3], algorithm: HashAlgorithm.SHA3_256)
let hashResult = Crypto.hash([1, 2, 3], algorithm: HashAlgorithm.SHA3_256)

Copilot uses AI. Check for mistakes.
}
}
`), 0644)

// Regression test files for nested import bug
_ = afero.WriteFile(mockFs, "Helper.cdc", []byte(`
Expand Down
36 changes: 36 additions & 0 deletions internal/cadence/linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/onflow/cadence/sema"
"github.com/onflow/cadence/stdlib"
"github.com/onflow/cadence/tools/analysis"
"github.com/onflow/flow-core-contracts/lib/go/contracts"
"github.com/onflow/flowkit/v2"
"golang.org/x/exp/maps"
)
Expand Down Expand Up @@ -319,6 +320,41 @@ func (l *linter) handleImport(
return sema.ElaborationImport{
Elaboration: helpersChecker.Elaboration,
}, nil
case stdlib.CryptoContractLocation:
cryptoChecker, ok := l.checkers[stdlib.CryptoContractLocation.String()]
if !ok {
cryptoCode := contracts.Crypto()
cryptoProgram, err := parser.ParseProgram(nil, cryptoCode, parser.Config{})
if err != nil {
return nil, err
Copy link

Copilot AI Dec 29, 2025

Choose a reason for hiding this comment

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

The error handling for parsing the Crypto contract could be more specific. Consider including the contract name in the error message to clarify which contract failed to parse, similar to the error message at line 333.

Suggested change
return nil, err
return nil, fmt.Errorf("cannot parse Crypto contract: %w", err)

Copilot uses AI. Check for mistakes.
}
if cryptoProgram == nil {
return nil, &sema.CheckerError{
Errors: []error{fmt.Errorf("cannot parse Crypto contract")},
}
}

cryptoChecker, err = sema.NewChecker(
cryptoProgram,
stdlib.CryptoContractLocation,
nil,
l.checkerStandardConfig,
)
if err != nil {
return nil, err
}

err = cryptoChecker.Check()
if err != nil {
return nil, err
}

l.checkers[stdlib.CryptoContractLocation.String()] = cryptoChecker
}

return sema.ElaborationImport{
Elaboration: cryptoChecker.Elaboration,
}, nil
default:
// Normalize relative path imports to absolute paths
if util.IsPathLocation(importedLocation) {
Expand Down
Loading