Quantcast
Viewing all articles
Browse latest Browse all 6140

How to locate a file with os.Stat() in WSL? [closed]

I was following frontend masters polyglot programming course where we built the Projector CLI Tool, a simple tool to manage enviroment variables. Anyway, that doesn't come to the point.

The thing is: we need to load some data from a json file, that we created before hand, and befere doing that we check if the file exists with the os.Stat() function that go provides us. For some reason it was giving some ENOENT errors, I thought it might be file permissions, since I'm running on WSL2 and probably dindn't setup my enviroment well.

I tried changing the permissions when creating the file, but that didn't work.

So, after trying some stuff and nothing was solving the problem, I decided to add the windows path to the Ubuntu WSL path, as you can see below, I don't even know if this was valid, and it worked

func NewProjector(config *Config) *Projector {    if _, err := os.Stat("C://wsl.localhost/Ubuntu-22.04/" + config.Config); os.IsNotExist(err) {        contents, err := os.ReadFile(config.Config)        if err != nil {            return defaultProjector(config)        }        var data Data        err = json.Unmarshal(contents, &data)        if err != nil {            return defaultProjector(config)        }        return &Projector{            data:   &data,            config: config,        }    }    return defaultProjector(config)}

Originally the function was like this, no "C://wsl.localhost..."

func NewProjector(config *Config) *Projector {    if _, err := os.Stat(config.Config); os.IsNotExist(err) {          \\...        }}

Can someone explain this to me? Is this some configuration to the WSL and Ubuntu that I missed? Is this the default Go behavior running in the WSL? Skill issue? Go issue? Windows issue?

This is the function that creates the file:

func (p *Projector) Save() error {    dir := path.Dir(p.config.Config)    if _, err := os.Stat(dir); os.IsNotExist(err) {        err = os.MkdirAll(dir, 0755)        if err != nil {            return err        }    }    jsonString, err := json.Marshal(p.data)    if err != nil {        return err    }    os.WriteFile(p.config.Config, jsonString, 0755)    return nil}

Running on WSL2, Ubuntu 22.04 distro, using VSCode.


Viewing all articles
Browse latest Browse all 6140

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>