streamich / memfs

JavaScript file system utilities
http://streamich.github.io/memfs/
Apache License 2.0
1.77k stars 130 forks source link

Dirent.#path changed when upgrading from 4.8.0 to 4.9.2 :: it doesn't include the last segment #1033

Closed marcog83 closed 7 months ago

marcog83 commented 7 months ago

Hi Streammich. We use memfs in our project to manage virtual file-system, and it fit 100% our needs. You saved a lot of headaches :) to our team.

I have this ViewsManager Class with getView method as below

export class ViewsManager {
  #fs: IFs;

  #volume: Volume = new Volume();

  constructor() {
    this.#fs = createFsFromVolume(this.#volume);
  }

  setViews(views: View[]) {
    const tree = createTree(views);
    this.#volume.fromJSON(tree, '/');
  }
  getViews() {
    const contents = (this.#fs.readdirSync('/', { recursive: true, withFileTypes: true }) ?? []) as Dirent[];
    return contents
      .filter((item) => item.isFile())
      .map((item) => {
        const file = this.#fs.readFileSync(item.path, 'utf8') ?? '';
        const content: View = JSON.parse(file.toString());
        content.path = item.path;
        return content;
      }).sort((a, b) => a.label.localeCompare(b.label, undefined, {
        sensitivity: 'base',
        caseFirst: 'upper',
        numeric: true,
      })) as View[];
  }
 //...

Then i have a jest test like this. internally createTree(views) reduces the array to

{
      '/my-root-saved-view': '{"path":"/my-root-saved-view","id":"0","data":"Data: 0","label":"Label: 0","updatedAt":0}',
      '/folder/nested/my-nested-saved-view': '{"path":"/folder/nested/my-nested-saved-view","id":"1","data":"Data: 1","label":"Label: 1","updatedAt":0}',
      '/folder/nested/2nd-nested-saved-view': '{"path":"/folder/nested/2nd-nested-saved-view","id":"2","data":"Data: 2","label":"Label: 2","updatedAt":0}'
    }
image

When i run the test with memfs 4.8.0 it works as expected, but if i use latest version 4.9.2 i get this error.

image

After logging the item i see this difference. it seems 4.8.1 includes the entire path in path field 4.9.2 doesn't include the last segment image

is it expected?

streamich commented 7 months ago

You are probably affected by this bug fix: https://github.com/streamich/memfs/pull/1024

marcog83 commented 7 months ago

Thanks for the replay. I' LL update my code to join parh+name then. I wanted to confirm that i was doing the right thing, and not Just something that happened to work 😀