prettier / prettier-vscode

Visual Studio Code extension for Prettier
https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode
MIT License
5.18k stars 460 forks source link

Ignore file not found if multiple workspace folders contain a file #2527

Closed samestep closed 2 years ago

samestep commented 2 years ago

Summary

Currently this extension uses getWorkspaceRelativePath to find .prettierignore:

https://github.com/prettier/prettier-vscode/blob/991a53f27df9f6d2abc782d8065512310c120983/src/PrettierEditService.ts#L401-L404

That function calls getWorkspaceFolder from the VS Code API:

https://github.com/prettier/prettier-vscode/blob/991a53f27df9f6d2abc782d8065512310c120983/src/util.ts#L20

The documentation doesn't specify which containing workspace folder is returned, but it seems to be the innermost one. This causes .prettierignore to not be found if it is in an outer workspace folder, a common situation with Lerna.

Github Repository to Reproduce Issue

https://github.com/samestep/prettier-vscode-ignore

Steps To Reproduce:

  1. Clone the repository linked above.
  2. Open foo.code-workspace from the cloned repository in VS Code.
  3. Open the file packages/foo/tsconfig.json.
  4. Run Format Document.

Expected result

Nothing happens, because packages/foo/tsconfig.json is in .prettierignore at the repository root.

Actual result

The file gets formatted with Prettier. Note that it is not a solution to simply move .prettierignore into packages/foo/ and remove the packages/foo/ prefix from all the lines, because then it would not be picked up by yarn format run from the repository root.

Additional information

VS Code Version: 1.66.2 (Universal)

Prettier Extension Version: 9.5.0

OS and version: macOS Monterey 12.3.1

Prettier Log Output

["INFO" - 4:15:43 PM] Extension Name: esbenp.prettier-vscode.
["INFO" - 4:15:43 PM] Extension Version: 9.5.0.
["DEBUG" - 4:15:43 PM] Enabling Prettier globally
{
  "languageSelector": [
    {
      "language": "javascript"
    },
    {
      "language": "mongo"
    },
    {
      "language": "javascriptreact"
    },
    {
      "language": "typescript"
    },
    {
      "language": "typescriptreact"
    },
    {
      "language": "json"
    },
    {
      "language": "jsonc"
    },
    {
      "language": "json5"
    },
    {
      "language": "css"
    },
    {
      "language": "postcss"
    },
    {
      "language": "less"
    },
    {
      "language": "scss"
    },
    {
      "language": "handlebars"
    },
    {
      "language": "graphql"
    },
    {
      "language": "markdown"
    },
    {
      "language": "mdx"
    },
    {
      "language": "html"
    },
    {
      "language": "vue"
    },
    {
      "language": "yaml"
    },
    {
      "language": "ansible"
    },
    {
      "language": "home-assistant"
    },
    {
      "language": "jsonc",
      "scheme": "vscode-userdata"
    }
  ],
  "rangeLanguageSelector": [
    {
      "language": "javascript"
    },
    {
      "language": "javascriptreact"
    },
    {
      "language": "typescript"
    },
    {
      "language": "typescriptreact"
    },
    {
      "language": "json"
    },
    {
      "language": "graphql"
    },
    {
      "language": "handlebars"
    }
  ]
}
["DEBUG" - 4:15:52 PM] Local prettier module path: '/Users/samueles/github/samestep/prettier-vscode-ignore/node_modules/prettier/index.js'
["DEBUG" - 4:15:52 PM] Using prettier version 2.6.2
["DEBUG" - 4:15:52 PM] Enabling Prettier for Workspace /Users/samueles/github/samestep/prettier-vscode-ignore/packages/foo
{
  "languageSelector": [
    {
      "pattern": "/Users/samueles/github/samestep/prettier-vscode-ignore/packages/foo/**/*.{js,_js,bones,cjs,es,es6,frag,gs,jake,jsb,jscad,jsfl,jsm,jss,mjs,njs,pac,sjs,ssjs,xsjs,xsjslib,wxs,js.flow,jsx,ts,mts,cts,tsx,json,avsc,geojson,gltf,har,ice,JSON-tmLanguage,mcmeta,tfstate,tfstate.backup,topojson,webapp,webmanifest,yy,yyp,jsonc,sublime-build,sublime-commands,sublime-completions,sublime-keymap,sublime-macro,sublime-menu,sublime-mousemap,sublime-project,sublime-settings,sublime-theme,sublime-workspace,sublime_metrics,sublime_session,json5,css,wxss,pcss,postcss,less,scss,handlebars,hbs,graphql,gql,graphqls,md,markdown,mdown,mdwn,mkd,mkdn,mkdown,ronn,scd,workbook,mdx,component.html,html,htm,html.hl,inc,xht,xhtml,mjml,vue,yml,mir,reek,rviz,sublime-syntax,syntax,yaml,yaml-tmlanguage,yaml.sed,yml.mysql}",
      "scheme": "file"
    },
    {
      "language": "javascript"
    },
    {
      "language": "mongo"
    },
    {
      "language": "javascriptreact"
    },
    {
      "language": "typescript"
    },
    {
      "language": "typescriptreact"
    },
    {
      "language": "json"
    },
    {
      "language": "jsonc"
    },
    {
      "language": "json5"
    },
    {
      "language": "css"
    },
    {
      "language": "postcss"
    },
    {
      "language": "less"
    },
    {
      "language": "scss"
    },
    {
      "language": "handlebars"
    },
    {
      "language": "graphql"
    },
    {
      "language": "markdown"
    },
    {
      "language": "mdx"
    },
    {
      "language": "html"
    },
    {
      "language": "vue"
    },
    {
      "language": "yaml"
    },
    {
      "language": "ansible"
    },
    {
      "language": "home-assistant"
    },
    {
      "language": "jsonc",
      "scheme": "vscode-userdata"
    }
  ],
  "rangeLanguageSelector": [
    {
      "language": "javascript"
    },
    {
      "language": "javascriptreact"
    },
    {
      "language": "typescript"
    },
    {
      "language": "typescriptreact"
    },
    {
      "language": "json"
    },
    {
      "language": "graphql"
    },
    {
      "language": "handlebars"
    }
  ]
}
["INFO" - 4:15:54 PM] Formatting file:///Users/samueles/github/samestep/prettier-vscode-ignore/packages/foo/tsconfig.json
["DEBUG" - 4:15:54 PM] Local prettier module path: '/Users/samueles/github/samestep/prettier-vscode-ignore/node_modules/prettier/index.js'
["DEBUG" - 4:15:54 PM] Using prettier version 2.6.2
["INFO" - 4:15:54 PM] Using ignore file (if present) at /Users/samueles/github/samestep/prettier-vscode-ignore/packages/foo/.prettierignore
["INFO" - 4:15:54 PM] File Info:
{
  "ignored": false,
  "inferredParser": "json"
}
["INFO" - 4:15:54 PM] No local configuration (i.e. .prettierrc or .editorconfig) detected, falling back to VS Code configuration
["INFO" - 4:15:54 PM] Prettier Options:
{
  "arrowParens": "always",
  "bracketSpacing": true,
  "endOfLine": "lf",
  "htmlWhitespaceSensitivity": "css",
  "insertPragma": false,
  "jsxBracketSameLine": false,
  "jsxSingleQuote": false,
  "printWidth": 80,
  "proseWrap": "preserve",
  "quoteProps": "as-needed",
  "requirePragma": false,
  "semi": true,
  "singleQuote": false,
  "tabWidth": 2,
  "trailingComma": "es5",
  "useTabs": false,
  "vueIndentScriptAndStyle": false,
  "filepath": "/Users/samueles/github/samestep/prettier-vscode-ignore/packages/foo/tsconfig.json",
  "parser": "json"
}
["INFO" - 4:15:54 PM] Formatting completed in 0.07ms.
github-actions[bot] commented 2 years ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.