Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LS] Allow importing any flow.json contracts using new import syntax #224

Merged
merged 4 commits into from
Oct 26, 2023
Merged
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
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
Loading