Bernhard Döbler
Bernhard Döbler

Reputation: 2116

VSCode files.associations

I have some .txt files in a subfolder of my project which contain SQL code. I want SQL syntax highlighting so I added this to my workspace folder settings:

{
    "files.associations": {
        "**/somefolder/*.txt": "sql" 
    }
}

This works when there are the two stars in front of the folder name. But somefolder actually is a folder direct under my project's root.

Why can I not write the glob like somefolder/**/*.txt?

Upvotes: 30

Views: 11409

Answers (5)

dodov
dodov

Reputation: 5844

This is not exactly related to the question of pattern-matching, but I had the issue of files.associations in folder settings not working at all.

If you have the following in my-project/.vscode/settings.json:

{
    "files.associations": {
        ...
    }
}

…and you open a workspace containing the folder, the setting won't apply and when you hover it, you'd see this message:

This setting cannot be applied in this workspace. It will be applied when you open the containing workspace folder directly.

As the message says, you'd have to right-click on the folder and open it in another VS Code window that is not related to a workspace. Only then it will work.

Upvotes: 0

subdeveloper
subdeveloper

Reputation: 2668

Context: Trying to answer the additional bounty question by @gremo:

TL;DR

This is due to full path vs filename matching behavior based on presence of /. The same is obscurely mentioned in VSCode docs as well. Read on for details.

Details:

As requested by @gremo, quoting from following reputable source:

Language Support in Visual Studio Code

Note that the pattern is a glob pattern that will match on the full path of the file if it contains a / and will match on the file name otherwise.

Read closely and you'll see the word full path of the file and file name stand out!

i.e. from above we can infer that depending on existence of / it will decide match mode/behavior of full path match vs partial/filename match!

This further makes more sense when we see that relative paths are not yet allowed, as mentioned at issue #12805 Allow workspace relative files.associations setting posted by @Bob_ in his answer.

Hopefully above sheds light on:
  1. Why pattern somefolder/*.txt doesn't match?

    • since there is a / in pattern, this will do a full path matching

    • in our case full path of loaded files will be: c:\scratch\VsCodePatternTest\somefolder\foo.txt and c:\scratch\VsCodePatternTest\foo.txt

    • so we're essentially trying to match above full path to pattern: somefolder/*.txt

    • Result: Neither matched!

    • Note: Technically this path will NEVER match anything, since on Windows/Linux/Mac u can't have an absolute path like above.. it needs to be rooted somehow:

              E.g. on Windows:
                 c:/somefolder  (or use `\\` for back slashes on windows)
              on Linux/Mac:
                 /somefolder
      
  2. Why pattern **/somefolder/*.txt match?

    • now again since there is a / in pattern, this will do a full path matching

    • like above again the full path of loaded files will be: c:\scratch\VsCodePatternTest\somefolder\foo.txt and c:\scratch\VsCodePatternTest\foo.txt

    • so we're essentially trying to match above full path to pattern: **/somefolder/*.txt

    • Result: Only one file match!

  3. Bonus: Why pattern foo.txt match?

    • now this time since there is NO / in pattern, this will do filename matching

    • like above again the full path of loaded files will be: c:\scratch\VsCodePatternTest\somefolder\foo.txt and c:\scratch\VsCodePatternTest\foo.txt

    • so we're essentially trying to match above filename i.e. foo.txt and foo.txt to pattern: foo.txt

    • Result: Both files match!

Tested above cases with:
  • Version/OS:

      VSCode 1.78.1 on Windows 10 x64
    
  • Project/Workspace structure:

      c:\scratch\VsCodePatternTest
          \.vscode
              settings.json
          \foo.txt
          \somefolder
              \foo.txt
    

Hope this clears things up!

Upvotes: 8

Bob__
Bob__

Reputation: 12759

It seems related to issue #12805 Allow workspace relative files.associations setting, where, in some comments, user bpasero (a staff member) states:

Since it is totally valid to open files without having a workspace open we cannot allow workspace relative paths here.

[...] allowing workspace relative paths is causing many issues because they do not work once you have no workspace open.

[...] For one a user might not easily understand the difference between workspace and global association setting, it is too subtle. And then while we could support ${workspaceRoot} it would not work when no workspace is open and again I fear this is too subtle...

Upvotes: 1

Lasithds
Lasithds

Reputation: 2291

For a jekyll site the following workspace setting works for me

{
    "files.associations": {
        "*.html": "jekyll",
        "**/_site/**/*.html": "html",
    }
}

Upvotes: 3

Fabio Tani
Fabio Tani

Reputation: 289

Seems like this new version of VSCode requires you to use the ** at the start regardless:

{
    "files.associations": {
        "**/somefolder/**/*.txt": "sql" 
    }
}

That should work

Upvotes: 28

Related Questions