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

No packages found for open file ... magefile.go. #519

Open
thomastthai opened this issue Feb 20, 2025 · 2 comments
Open

No packages found for open file ... magefile.go. #519

thomastthai opened this issue Feb 20, 2025 · 2 comments
Labels

Comments

@thomastthai
Copy link

thomastthai commented Feb 20, 2025

Bug Description
In Neovim, there is a diagnostic warning at the package main: No packages found for open file ... magefile.go.

What did you do?
The magefile.go is placed in the project-root directory. Running mage target works fine. What is causing that diagnostic error?

What did you expect to happen?
No diagnostic warning.

What actually happened?
A diagnostic warning at the package main: No packages found for open file ... magefile.go.

Image

Environment

  • Mage Version:
 ❯ mage --version
Mage Build Tool 1.15.0
Build Date: 2023-05-11T15:35:27Z
Commit: 9e91a03
built with: go1.23.1
  • OS: macOS Sequoia 15.3.1
  • Go: go version go1.24.0 darwin/arm64

Additional context
n/a

@thomastthai
Copy link
Author

thomastthai commented Feb 20, 2025

Moving magefile.go to magefiles/magefile.com didn't resolve the diagnostic warning.

Image

@thomastthai
Copy link
Author

thomastthai commented Feb 20, 2025

Based on some reports, it's likely due to gopls?

Solutions

magefiles/directory and no build tag

Move the magefile.go to a subdirectory and remove the Go build tag comment. This resolved the diagnostic warning.

Image

Configure gopls setting for your IDE

In your IDE, configure gopls to set standaloneTags with the values ignore, and mage.

Neovim with mason_lspconfig

...
  		mason_lspconfig.setup_handlers({
			-- default handler for installed servers
			function(server_name)
				if server_name ~= "gopls" then -- Skip default handler for golang (gopls)
					lspconfig[server_name].setup({
						capabilities = capabilities,
					})
				end
			end,
			["golang"] = function() -- Specific handler for golang (gopls)
				lspconfig["gopls"].setup({
					capabilities = capabilities,
					settings = {
						gopls = {
							standaloneTags = { "ignore", "mage" },
						},
					},
				})
			end,
...

Neovim with LSPConfig

-- init.lua

local lspconfig = require('lspconfig')

lspconfig.gopls.setup {
  settings = {
    gopls = {
      standaloneTags = { "ignore", "mage" },
    },
  },
}

Restart Neovim for the changes to take effect.

VS Code:

You can configure gopls in VS Code to set the standaloneTags to ["ignore", "mage"] by modifying VS Code's settings.json file. Here's how to do it:

Steps to configure standaloneTags for gopls in VS Code:

You can access VS Code settings in a couple of ways. Here are both options:

Option 1: Using VS Code Settings UI

This is often the easiest way to find and modify settings:

  1. Open VS Code Settings:

    • On macOS: Go to Code > Settings > Settings (or use the shortcut Cmd + ,).
    • On Windows/Linux: Go to File > Preferences > Settings (or use the shortcut Ctrl + ,).
  2. Search for gopls standaloneTags: In the Settings search bar at the top, type gopls standaloneTags. This will filter the settings to show the relevant gopls setting.

  3. Locate the Go > Tools > Gopls > Standalone Tags setting: You should see the setting listed. It likely currently has a default value (probably ["ignore"]).

  4. Edit in settings.json: To directly modify the JSON configuration (which gives you more control and is often clearer for complex settings), click on the "Edit in settings.json" icon. This icon usually looks like a pencil or a document with a pencil, and it's located to the left of the setting name when you hover over the setting or its description.

    This action will open your settings.json file (either User or Workspace settings, depending on which tab you are in in the Settings UI).

  5. Modify standaloneTags in settings.json: In your settings.json file, you will likely see a section for "gopls". If you don't have a "gopls" section yet, you'll need to add it. Within the "gopls" section, add or modify the "standaloneTags" setting like this:

    {
        "gopls": {
            "standaloneTags": ["ignore", "mage"]
        }
    }
    • If you already have a "gopls" section, just add or modify the "standaloneTags" line within it.
    • If you don't have a "gopls" section yet, you can add the entire block above.

Option 2: Editing settings.json Directly

If you prefer to directly edit the settings.json file, you can do that as well:

  1. Open settings.json:

    • User Settings (applies to all VS Code projects):
      • macOS: Press Cmd + Shift + P and type "Preferences: Open Settings (JSON)" and press Enter.
      • Windows/Linux: Press Ctrl + Shift + P and type "Preferences: Open Settings (JSON)" and press Enter.
    • Workspace Settings (applies only to the current project): This is often recommended for project-specific settings like standaloneTags.
      • If you want to set this only for your current project, navigate to your project's root directory in VS Code's Explorer.
      • Then, in the Command Palette (Cmd + Shift + P or Ctrl + Shift + P), type "Preferences: Open Workspace Settings (JSON)" and press Enter. This will create a .vscode/settings.json file in your project root if it doesn't already exist, or open it if it does.
  2. Add or Modify gopls Configuration: In the settings.json file that opens (either User or Workspace), add or modify the "gopls" section as shown in Option 1, step 5:

    {
        "gopls": {
            "standaloneTags": ["ignore", "mage"]
        }
    }

Choosing User or Workspace Settings:

  • Workspace Settings (.vscode/settings.json): Recommended if you only need this standaloneTags configuration for specific projects (like projects using mage). Workspace settings are project-specific and stored in your project directory.
  • User Settings (global settings.json): If you want gopls to always recognize "mage" as a standalone tag for all Go projects you open in VS Code, use User settings.

After Editing settings.json:

  1. Save the settings.json file: VS Code should automatically detect the changes and apply them.
  2. Restart VS Code or Reload Window (optional but recommended): While VS Code usually applies settings changes immediately, sometimes restarting VS Code or reloading the window (Cmd + Shift + P or Ctrl + Shift + P, then type "Reload Window") ensures that gopls fully picks up the new configuration.

Verification:

To verify that the standaloneTags setting is applied:

  1. Open a magefile.go in your VS Code project.
  2. Check for "Run" and "Debug" options: See if VS Code now provides "Run" and "Debug" buttons or options directly in the editor for your magefile.go. If standaloneTags is correctly configured, VS Code should recognize magefile.go as a standalone main file and enable these options.
  3. Check gopls behavior: You should see that gopls is active for your magefile.go (e.g., code completion, diagnostics, etc. should be working).

Other IDE or text editors

You can follow similar steps to successfully configure gopls in your favorite editor to recognize mage as a standalone tag by setting the standaloneTags setting.

If this info isn't already in the docs or website for mage, it would benefit the community if it was added.

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

1 participant