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

Suggest functions in imported modules with the same name as an unknown variable #2697

Open
JS-JeevaSaravanan opened this issue Mar 13, 2024 · 4 comments
Labels
good first issue Good for newcomers help wanted Contributions encouraged priority:medium

Comments

@JS-JeevaSaravanan
Copy link

Description of the Problem:

While working with Gleam version 1.0.0, I attempted to use a function to print a list of numbers to the console but mistakenly omitted the package prefix from the function call. Instead of suggesting the correct function from the imported package, the compiler suggested an unrelated term.

Code to Reproduce:

import gleam/io

pub fn main() {
  let numbers = [1, 2, 3, 4, 5]
  debug(numbers) // Intended to use io.debug but mistakenly omitted 'io.'
}

Expected Behavior:

I expected the compiler to suggest using io.debug or another relevant function from the gleam/io package since it was imported and contained functions for console output.

Actual Behavior:

The compiler identified debug as an unknown variable and suggested unrelated terms, missing the opportunity to suggest the correct function from an already imported package.

Error Message:

error: Unknown variable
  ┌─ /path/to/project/src/my_project.gleam:5:3
  │
5 │   debug(numbers)
  │   ^^^^^ Did you mean `True`?

Suggested Improvement:

Enhance the compiler's error suggestions to consider functions available in the imported modules/packages, especially when an unknown function or variable matches a name in those imports. This feature could significantly improve developer experience by providing more contextually relevant suggestions and reducing potential confusion, particularly for newcomers or in cases of simple typos.

Additional Context:

This issue arose when I tried to print a list of numbers without correctly specifying the io module prefix for the debug function. The current compiler suggestions did not help in guiding me towards the correct usage of imported functions.

Gleam Version: 1.0.0
Operating System: macOS Ventura (Version 13.2)

image

@lpil lpil changed the title Compiler Does Not Suggest Functions from Imported Packages for Unknown Variables/Functions Suggest functions in imported modules with the same name as an unknown variable Mar 13, 2024
@lpil
Copy link
Member

lpil commented Mar 13, 2024

Good idea! Thank you

@lpil lpil added help wanted Contributions encouraged good first issue Good for newcomers priority:medium labels Mar 13, 2024
@le-incroyable1-dev
Copy link

le-incroyable1-dev commented Mar 14, 2024

Hi @lpil and everyone, I'm interested in supporting Gleam. It's great to see all the work being done, and it's my first time contributing here. Could anyone help me get started and work on this issue? I think I need to make changes in error.rs??

@lpil
Copy link
Member

lpil commented Mar 14, 2024

Wonderful, thank you.

You'd need to find where the error is emitted and at that point look to see which (if any) of the imported modules have functions with that name with the same number of arguments. If any are found then you can add the names of the modules to the error and then when printing the error include them in the error message in some way.

shellbear added a commit to shellbear/gleam that referenced this issue Mar 15, 2024
…iable

This improves the overall compiler unknown variables suggestions by listing
all imported modules values in the scope so it can now recommands imports.

For example with the following code (described in GitHub issue gleam-lang#2697):
```
import gleam/io

pub fn main() {
  let numbers = [1, 2, 3, 4, 5]
  debug(numbers) // Intended to use io.debug but mistakenly omitted 'io.'
}
```

The current version of the compiler provides the following error:
```
error: Unknown variable
  ┌─ /path/to/project/src/my_project.gleam:5:3
  │
5 │   debug(numbers)
  │   ^^^^^ Did you mean `True`?
```

With my changes the suggestions are a bit smarter:
```
error: Unknown variable
  ┌─ /Users/shellbear/code/gleamio/src/gleamio.gleam:5:3
  │
5 │   debug(numbers) // Intended to use io.debug but mistakenly omitted 'io.'
  │   ^^^^^ Did you mean `io.debug`?

The name `debug` is not in scope here.
```
shellbear added a commit to shellbear/gleam that referenced this issue Mar 15, 2024
…iable

This improves the overall compiler unknown variables suggestions by listing
all imported modules values in the scope so it can now recommends imports.

For example with the following code (described in GitHub issue gleam-lang#2697):
```
import gleam/io

pub fn main() {
  let numbers = [1, 2, 3, 4, 5]
  debug(numbers) // Intended to use io.debug but mistakenly omitted 'io.'
}
```

The current version of the compiler provides the following error:
```
error: Unknown variable
  ┌─ /path/to/project/src/my_project.gleam:5:3
  │
5 │   debug(numbers)
  │   ^^^^^ Did you mean `True`?
```

With my changes the suggestions are a bit smarter:
```
error: Unknown variable
  ┌─ /Users/shellbear/code/gleamio/src/gleamio.gleam:5:3
  │
5 │   debug(numbers) // Intended to use io.debug but mistakenly omitted 'io.'
  │   ^^^^^ Did you mean `io.debug`?

The name `debug` is not in scope here.
```
shellbear added a commit to shellbear/gleam that referenced this issue Mar 15, 2024
…iable

This improves the overall compiler unknown variables suggestions by listing
all imported modules values in the scope so it can now recommends imports.

For example with the following code (described in GitHub issue gleam-lang#2697):
```
import gleam/io

pub fn main() {
  let numbers = [1, 2, 3, 4, 5]
  debug(numbers) // Intended to use io.debug but mistakenly omitted 'io.'
}
```

The current version of the compiler provides the following error:
```
error: Unknown variable
  ┌─ /path/to/project/src/my_project.gleam:5:3
  │
5 │   debug(numbers)
  │   ^^^^^ Did you mean `True`?
```

With my changes the suggestions are a bit smarter:
```
error: Unknown variable
  ┌─ /Users/shellbear/code/gleamio/src/gleamio.gleam:5:3
  │
5 │   debug(numbers) // Intended to use io.debug but mistakenly omitted 'io.'
  │   ^^^^^ Did you mean `io.debug`?

The name `debug` is not in scope here.
```
@JS-JeevaSaravanan
Copy link
Author

Thank you @lpil and @shellbear for resolving this issue ! Good work guys !

shellbear added a commit to shellbear/gleam that referenced this issue Mar 18, 2024
…iable

This improves the overall compiler unknown variables suggestions by listing
all imported modules values in the scope so it can now recommends imports.

For example with the following code (described in GitHub issue gleam-lang#2697):
```
import gleam/io

pub fn main() {
  let numbers = [1, 2, 3, 4, 5]
  debug(numbers) // Intended to use io.debug but mistakenly omitted 'io.'
}
```

The current version of the compiler provides the following error:
```
error: Unknown variable
  ┌─ /path/to/project/src/my_project.gleam:5:3
  │
5 │   debug(numbers)
  │   ^^^^^ Did you mean `True`?
```

With my changes the suggestions are a bit smarter:
```
error: Unknown variable
  ┌─ /Users/shellbear/code/gleamio/src/gleamio.gleam:5:3
  │
5 │   debug(numbers) // Intended to use io.debug but mistakenly omitted 'io.'
  │   ^^^^^ Did you mean `io.debug`?

The name `debug` is not in scope here.
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Contributions encouraged priority:medium
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants
@lpil @le-incroyable1-dev @JS-JeevaSaravanan and others