crystal-lang-tools / vscode-crystal-lang

Yet another VSCode extension for Crystal Programming Language
https://marketplace.visualstudio.com/items?itemName=crystal-lang-tools.crystal-lang
MIT License
275 stars 57 forks source link

Classes imported from shards throw a "undefined constant" error #194

Open wJoenn opened 4 months ago

wJoenn commented 4 months ago

Describe the bug I have a small api that uses the jennifer ORM

The shard is required from it's initializer file

# config/initializers/database.cr
require "colorize"
require "jennifer"
require "jennifer/adapter/postgres"

Which itself is imported by the config file

# config/config.cr
require "./initializers/**"
require "../src/models/*"

But in my Task model I get an error saying the Jennifer::Model::Base class is undefined

# src/models/task.cr
class Task < Jennifer::Model::Base
end

The code works though as tests passes

This is true for any class coming from an installed shard which is not required in the file it's being used as far as I can tell

To Reproduce Create a directory, install shard, require it in a config file and use it from a file that requires the config file

Expected behavior It shouldn't throw an error

Screenshots image

Desktop (please complete the following information):

nobodywasishere commented 3 months ago

This is probably due to the wrong main file being set. Some fixes should be coming soon but in the meantime can you post the contents of the crystal output channel when this happens, and your projects file structure?

wJoenn commented 3 months ago

Output

[Crystal] loading lsp /home/joenn/.asdf/plugins/crystal/bin/crystalline
[Info  - 11:37:35 PM] LSP server is ready.
[Spec] (kemal) $ /home/joenn/.asdf/shims/crystal spec --junit_output /tmp/202433-2488-qk5rfi.6yvnj.xml --no-color   --dry-run

File Structure

|-- ROOT
|   |-- bin
|   |-- config
|   |   |-- initializers
|   |   |   |-- cors.cr                  <-- undefined constant Kemal::Handler error
|   |   |   |-- database.cr              <-- require jennifer here
|   |   |-- config.cr                    <-- require Kemal then all initializers here
|   |-- db
|   |   |-- migrations
|   |   |   |-- create_tasks.cr          <-- undefined constant Jennifer::Migration::Base error
|   |-- lib
|   |   |-- jennifer
|   |   |-- kemal
|   |-- spec
|   |-- src
|   |   |-- controller
|   |   |-- models
|   |   |   |-- user.cr                  <-- undefined constant Jennifer::Model::Base error
|   |   |-- services
|   |   |-- app.cr                       <-- require config/config.cr
|   |-- shards.lock
|   |-- shards.yml

Not that all the errors I get are from classes I inherit from.

class CorsHandler < Kemal::Handler
end

class CreateTasks < Jennifer::Migration::Base
end

class User < Jennifer::Model::Base
end
nobodywasishere commented 3 months ago

As you're using Crystalline, the issue either lies with that or with how you've set it up. Do these errors still come up when the LSP is disabled?

wJoenn commented 3 months ago

I don't get those errors when disabling Crystalline but then I'm back to this issue https://github.com/crystal-lang-tools/vscode-crystal-lang/issues/185

nobodywasishere commented 2 months ago

When using crystalline, try setting an entrypoint using the process described here