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

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

Open
wJoenn opened this issue Mar 2, 2024 · 5 comments
Open

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

wJoenn opened this issue Mar 2, 2024 · 5 comments
Assignees
Labels

Comments

@wJoenn
Copy link

wJoenn commented Mar 2, 2024

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):

  • OS: Windows 10 + WSL2 Ubuntu22
  • Crystal version: v1.11.2
  • This plugin version: 0.9.3
@nobodywasishere
Copy link
Member

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
Copy link
Author

wJoenn commented Apr 3, 2024

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
Copy link
Member

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
Copy link
Author

wJoenn commented Apr 3, 2024

I don't get those errors when disabling Crystalline but then I'm back to this issue #185

@nobodywasishere
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants