Skip to content

Commit

Permalink
Merge pull request #224 from onflow/jribbink/fix-importing-aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent authored Oct 26, 2023
2 parents 846cd46 + a5c0765 commit 4193db8
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 10 deletions.
34 changes: 25 additions & 9 deletions languageserver/integration/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ package integration
import (
"context"
"fmt"
"github.com/onflow/flow-cli/flowkit/accounts"
"github.com/onflow/flow-cli/flowkit/transactions"
"net/url"
"os"
"path/filepath"

"github.com/onflow/cadence"
"github.com/onflow/flow-cli/flowkit"
"github.com/onflow/flow-cli/flowkit/accounts"
"github.com/onflow/flow-cli/flowkit/config"
"github.com/onflow/flow-cli/flowkit/gateway"
"github.com/onflow/flow-cli/flowkit/output"
"github.com/onflow/flow-cli/flowkit/transactions"
"github.com/onflow/flow-go-sdk"
"github.com/onflow/flow-go-sdk/crypto"
)
Expand Down Expand Up @@ -393,23 +393,39 @@ func (f *flowkitClient) createSigner(address flow.Address) (*accounts.Account, e
}

func (f *flowkitClient) GetCodeByName(name string) (string, error) {
contracts, err := f.state.DeploymentContractsByNetwork(config.EmulatorNetwork)
// Try to find the contract by name
contract, err := f.state.Contracts().ByName(name)
if err != nil {
return "", err
return "", fmt.Errorf("couldn't find the contract by import identifier: %s", name)
}

for _, contract := range contracts {
if name == contract.Name {
return string(contract.Code()), nil
}
// If no location is set, return an error
if contract.Location == "" {
return "", fmt.Errorf("source file could not be found for import identifier: %s", name)
}

return "", fmt.Errorf(fmt.Sprintf("couldn't find the contract by import identifier: %s", name))
// Resolve the contract source code from file location
code, err := f.getCodeFromLocation(name, contract.Location)
if err != nil {
return "", err
}
return code, nil
}

// Helpers
//

// Helper function to get code from a source file location
func (f *flowkitClient) getCodeFromLocation(name, location string) (string, error) {
dir := filepath.Dir(f.getConfigPath())
path := filepath.Join(dir, location)
code, err := f.loader.ReadFile(path)
if err != nil {
return "", err
}
return string(code), nil
}

// resolveFilename helper converts the transaction file to a relative location to config file
func resolveFilename(configPath string, path string) (string, error) {
if filepath.Dir(configPath) == "." { // if flow.json is passed as relative use current dir
Expand Down
1 change: 1 addition & 0 deletions languageserver/test/a.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub contract A {}
6 changes: 5 additions & 1 deletion languageserver/test/flow.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
},
"contracts": {
"Foo": "./foo.cdc",
"Bar": "./bar.cdc"
"Bar": "./bar.cdc",
"A": {
"source": "./a.cdc",
"aliases": {}
}
},
"networks": {
"emulator": "127.0.0.1:3569",
Expand Down
17 changes: 17 additions & 0 deletions languageserver/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,23 @@ describe("diagnostics", () => {
expect(script.diagnostics).toHaveLength(0)
})

test("script with string import non-deployment contract", async () => {
const scriptName = "script";
const scriptCode = `
import "A"
pub fun main() { }
`;

let docNotifications = await testImports([
{ name: scriptName, code: scriptCode },
]);

let script = await docNotifications.find((n) => n.name == scriptName)
.notification
expect(script.uri).toEqual(`file://${scriptName}.cdc`)
expect(script.diagnostics).toHaveLength(0)
})

test("script import failure", async() => {
const contractName = "foo"
const scriptName = "script"
Expand Down

0 comments on commit 4193db8

Please sign in to comment.