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

bug: Formatter not respecting prettier.config.js file #618

Closed
2 tasks done
Maduki-tech opened this issue Jan 5, 2025 · 13 comments
Closed
2 tasks done

bug: Formatter not respecting prettier.config.js file #618

Maduki-tech opened this issue Jan 5, 2025 · 13 comments
Labels
bug Something isn't working

Comments

@Maduki-tech
Copy link

Neovim version (nvim -v)

NVIM v0.11.0-dev-1422+g35247b00a4 Build type: RelWithDebInfo LuaJIT 2.1.1734355927

Operating system/version

MacOS 15.2

Read debugging tips

Add the debug logs

  • I have set log_level = vim.log.levels.DEBUG and pasted the log contents below.

Log file

No error here in the Debug for me.

Describe the bug

I have my basic NextJS application. npx create-next-app@latest
Then i added my default prettier.config.js file.

/** @type {import('prettier').Config & import('prettier-plugin-tailwindcss').PluginOptions} */
export default {
    tabWidth: 2,
    plugins: ["prettier-plugin-tailwindcss"],
};

When I run my format command, is is not respecting this file.

What is the severity of this bug?

minor (annoyance)

Steps To Reproduce

  1. nvim -u repro.lua
  2. run any js/ts/json file

Expected Behavior

To format the files based on the provided configuration in the directory

Minimal example file

/** @type {import('prettier').Config & import('prettier-plugin-tailwindcss').PluginOptions} */
export default {
    tabWidth: 2,
    plugins: ["prettier-plugin-tailwindcss"],
};

You can use this file, it is already in js.

Minimal init.lua

-- DO NOT change the paths and don't remove the colorscheme
local root = vim.fn.fnamemodify("./.repro", ":p")

-- set stdpaths to use .repro
for _, name in ipairs({ "config", "data", "state", "cache" }) do
  vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

-- bootstrap lazy
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system({
    "git",
    "clone",
    "--filter=blob:none",
    "--single-branch",
    "https://github.com/folke/lazy.nvim.git",
    lazypath,
  })
end
vim.opt.runtimepath:prepend(lazypath)

-- install plugins
local plugins = {
  "folke/tokyonight.nvim",
  {
    "stevearc/conform.nvim",
    config = function()
      require("conform").setup({
        log_level = vim.log.levels.DEBUG,
        -- add your config here
  opts = {
    notify_on_error = true,
    format_on_save = function(bufnr)
      -- Disable "format_on_save lsp_fallback" for languages that don't
      -- have a well standardized coding style. You can add additional
      -- languages here or re-enable it for the disabled ones.
      local disable_filetypes = { sql = true }
      return {
        timeout_ms = 500,
        lsp_fallback = not disable_filetypes[vim.bo[bufnr].filetype],
      }
    end,
    stop_after_first = true,
    formatters_by_ft = {
      lua = { 'stylua' },
      html = { 'prettierd' },
      javascript = { 'prettierd' },
      typescript = { 'prettierd' },
      typescriptreact = { 'prettierd' },
      javascriptreact = { 'prettierd' },
    },
  },

  keys = {
    {
      '<leader>tf',
      function()
        require('conform').format { async = true, lsp_fallback = true }
      end,
      mode = '',
      desc = '[F]ormat buffer',
    },
  },

      })
    end,
  },
  -- add any other plugins here
}
require("lazy").setup(plugins, {
  root = root .. "/plugins",
})

vim.cmd.colorscheme("tokyonight")
-- add anything else here

Additional context

No response

@Maduki-tech Maduki-tech added the bug Something isn't working label Jan 5, 2025
@stevearc
Copy link
Owner

stevearc commented Jan 5, 2025

Can you follow the steps in debugging tips to discover:

  1. the command that conform is running to format the files
  2. find a command that you can run in the terminal that will format the files correctly

@stevearc stevearc added the question Further information is requested label Jan 5, 2025
@Maduki-tech
Copy link
Author

I have enabled the log_level but there is still no entry in the file. It is completely empty.

return { -- Autoformat
  'stevearc/conform.nvim',
  lazy = false,
  keys = {
    {
      '<leader>tf',
      function()
        require('conform').format { async = true, lsp_fallback = true }
      end,
      mode = '',
      desc = '[F]ormat buffer',
    },
  },
  log_level = vim.log.levels.DEBUG,
  opts = {
    notify_on_error = true,
    format_on_save = function(bufnr)
      -- Disable "format_on_save lsp_fallback" for languages that don't
      -- have a well standardized coding style. You can add additional
      -- languages here or re-enable it for the disabled ones.
      local disable_filetypes = { sql = true }
      return {
        timeout_ms = 500,
        lsp_fallback = not disable_filetypes[vim.bo[bufnr].filetype],
      }
    end,
    stop_after_first = true,
    formatters_by_ft = {
      lua = { 'stylua' },
      html = { 'prettierd' },
      javascript = { 'prettierd' },
      typescript = { 'prettierd' },
      typescriptreact = { 'prettierd' },
      javascriptreact = { 'prettierd' },
    },
  },
}

When I run in my cmd

▶ npx prettier . --write --config prettier.config.js

It is formating my files.

@github-actions github-actions bot removed the question Further information is requested label Jan 5, 2025
@stephen776
Copy link

can confirm. same issue happening for me.

@stevearc
Copy link
Owner

stevearc commented Jan 6, 2025

Okay looking closer, your config is all over the place. The options aren't in the right place at all. Try this minimal init and see if that produces logging output

-- DO NOT change the paths and don't remove the colorscheme
local root = vim.fn.fnamemodify("./.repro", ":p")

-- set stdpaths to use .repro
for _, name in ipairs({ "config", "data", "state", "cache" }) do
  vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

-- bootstrap lazy
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system({
    "git",
    "clone",
    "--filter=blob:none",
    "--single-branch",
    "https://github.com/folke/lazy.nvim.git",
    lazypath,
  })
end
vim.opt.runtimepath:prepend(lazypath)

-- install plugins
local plugins = {
  "folke/tokyonight.nvim",
  {
    "stevearc/conform.nvim",
    lazy = false,
    keys = {
      {
        "<leader>tf",
        function() require("conform").format({ async = true, lsp_fallback = true }) end,
        mode = "",
        desc = "[F]ormat buffer",
      },
    },
    config = function()
      require("conform").setup({
        log_level = vim.log.levels.DEBUG,
        notify_on_error = true,
        format_on_save = function(bufnr)
          -- Disable "format_on_save lsp_fallback" for languages that don't
          -- have a well standardized coding style. You can add additional
          -- languages here or re-enable it for the disabled ones.
          local disable_filetypes = { sql = true }
          return {
            timeout_ms = 500,
            lsp_fallback = not disable_filetypes[vim.bo[bufnr].filetype],
          }
        end,
        formatters_by_ft = {
          lua = { "stylua" },
          html = { "prettierd" },
          javascript = { "prettierd" },
          typescript = { "prettierd" },
          typescriptreact = { "prettierd" },
          javascriptreact = { "prettierd" },
        },
      })
    end,
  },
}
require("lazy").setup(plugins, {
  root = root .. "/plugins",
})

vim.cmd.colorscheme("tokyonight")

@stevearc stevearc added the question Further information is requested label Jan 6, 2025
@Maduki-tech
Copy link
Author

Good Morning.

Yes this produces output for me.

2025-01-06 08:21:41[DEBUG] Running formatters on /Users/davidschluter/personal/fullstack/WorkChrono/frontend/prettier.config.js: { "prettierd" }
2025-01-06 08:21:41[INFO] Run prettierd on /Users/davidschluter/personal/fullstack/WorkChrono/frontend/prettier.config.js
2025-01-06 08:21:41[DEBUG] Run command: { "/Users/davidschluter/.local/share/nvim/mason/bin/prettierd", "/Users/davidschluter/personal/fullstack/WorkChrono/frontend/prettier.config.js" }
2025-01-06 08:21:41[DEBUG] Run CWD: /Users/davidschluter/personal/fullstack/WorkChrono/frontend
2025-01-06 08:21:41[DEBUG] prettierd exited with code 0

@github-actions github-actions bot removed the question Further information is requested label Jan 6, 2025
@stephen776
Copy link

I recall this happening before and this was the fix

@ro0gr
Copy link
Contributor

ro0gr commented Jan 6, 2025

Yes this produces output for me.

The output looks good to me.

Do you still have an issue with the prettier-plugin-tailwindcss using the conform, but don't see the issue running the prettier manually? Or the issue can be closed?

@stephen776
Copy link

I am using LazyVim with TS and Prettier properly configured (this setup has been working and recently stopped). Here's what my log looks like:

Log file: /Users/stephen776/.local/state/nvim/conform.log
          2025-01-06 09:15:49[DEBUG] Running LSP formatter on /Users/stephen776/projects/my-project/apps/web/app/features/auth/routes.ts
          2025-01-06 09:18:42[DEBUG] Running LSP formatter on /Users/stephen776/projects/my-project/apps/web/app/features/auth/routes.ts
          2025-01-06 09:18:42[DEBUG] Running LSP formatter on /Users/stephen776/projects/my-project/apps/web/app/features/auth/routes.ts

Formatters for this buffer:
LSP: null-ls, vtsls, eslint
prettier ready (json, typescriptreact, javascriptreact, vue, css, markdown, handlebars, less, markdown.mdx, scss, javascript, html, jsonc, typescript, graphql, yaml) /Users/stephen776/projects/my-project/node_modules/.bin/prettier
biome unavailable: Root directory not found

@Maduki-tech
Copy link
Author

Yes this produces output for me.

The output looks good to me.

Do you still have an issue with the prettier-plugin-tailwindcss using the conform, but don't see the issue running the prettier manually? Or the issue can be closed?

@ro0gr The bug is still present. The output looks right I Agree, but the formatter is not doing anything (at least the indentation is not working correctly when switching between tabWidth: 2 and tabWidth: 4)

Edit: Also the Quotes are not working for me.

@Maduki-tech
Copy link
Author

Maduki-tech commented Jan 6, 2025

I am using LazyVim with TS and Prettier properly configured (this setup has been working and recently stopped). Here's what my log looks like:

For me it also worked Fine for months. Recently I updated my Plugins and then it stopped working.

@stevearc
Copy link
Owner

stevearc commented Jan 7, 2025

@Maduki-tech the command line invocation that you have that formats the files correctly uses npx and prettier, while the conform formatter using prettierd installed with mason. Can you either reconfigure conform to use the package-local prettier command or try running the mason-installed prettierd command in the terminal?

@stevearc stevearc added the question Further information is requested label Jan 7, 2025
@Maduki-tech
Copy link
Author

Hey, good you saw this. I think now, that I started to use prettier with my local path.
This is my debug output now

2025-01-07 16:10:10[INFO] Run prettier on /Users/davidschluter/personal/fullstack/WorkChrono/frontend/prettier.config.js
2025-01-07 16:10:10[DEBUG] Run command: { "/Users/davidschluter/personal/fullstack/WorkChrono/frontend/node_modules/.bin/prettier", "--stdin-filepath", "/Users/davidschluter/personal/fullstack/WorkChrono/frontend/prettier.config.js" }
2025-01-07 16:10:10[DEBUG] Run CWD: /Users/davidschluter/personal/fullstack/WorkChrono/frontend
2025-01-07 16:10:10[DEBUG] prettier exited with code 0

Changed this in my config

  formatters_by_ft = {
    lua = { 'stylua' },
    html = { 'prettier' },
    javascript = { 'prettier' },
    typescript = { 'prettier' },
    typescriptreact = { 'prettier' },
    javascriptreact = { 'prettier' },
  },

@github-actions github-actions bot removed the question Further information is requested label Jan 7, 2025
@Maduki-tech
Copy link
Author

Seems like you can not use useTabs and tab width at the same time. Removing useTabs gained me the control over everything.....

Sorry for time waste!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants