Repository: nuxt/nuxt.js
Pull request #8832 (2021-02-15 17:56:24)

fix(builder): use path.resolve for pages dir
<!--- Provide a general summary of your changes in the title above -->

## Types of changes
<!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: -->
- [x] Bug fix (a non-breaking change which fixes an issue)


## Description
<!--- Describe your changes in detail -->
<!--- Why is this change required? What problem does it solve? -->
<!--- If it resolves an open issue, please link to the issue here. For example "Resolves: #1337" -->
This allow to give a `pages/` directory outside the `srcDir`. It should not break anything.

## Checklist:
<!--- Put an `x` in all the boxes that apply. -->
<!--- If your change requires a documentation PR, please link it appropriately -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! -->
- [ ] All new and existing tests are passing.




Patch:
diff --git a/packages/builder/src/builder.js b/packages/builder/src/builder.js
index 7e9b8db73..5ebf39581 100644
--- a/packages/builder/src/builder.js
+++ b/packages/builder/src/builder.js
@@ -180,7 +180,7 @@ export default class Builder {
 
     if (
       !this._nuxtPages ||
-      await fsExtra.exists(path.join(this.options.srcDir, this.options.dir.pages))
+      await fsExtra.exists(path.resolve(this.options.srcDir, this.options.dir.pages))
     ) {
       return
     }


Test patch:
diff --git a/packages/builder/test/builder.build.test.js b/packages/builder/test/builder.build.test.js
index d63755ef4..51a84f2eb 100644
--- a/packages/builder/test/builder.build.test.js
+++ b/packages/builder/test/builder.build.test.js
@@ -16,11 +16,13 @@ jest.mock('@nuxt/webpack')
 describe('builder: builder build', () => {
   beforeAll(() => {
     jest.spyOn(path, 'join').mockImplementation((...args) => `join(${args.join(', ')})`)
+    jest.spyOn(path, 'resolve').mockImplementation((...args) => `resolve(${args.join(', ')})`)
     r.mockImplementation((...args) => `r(${args.join(', ')})`)
   })
 
   afterAll(() => {
     path.join.mockRestore()
+    path.resolve.mockRestore()
   })
 
   beforeEach(() => {
@@ -143,11 +145,12 @@ describe('builder: builder build', () => {
     await builder.validatePages()
 
     expect(builder._nuxtPages).toEqual(true)
-    expect(path.join).toBeCalledTimes(2)
-    expect(path.join).nthCalledWith(1, '/var/nuxt/src', '/var/nuxt/src/pages')
-    expect(path.join).nthCalledWith(2, '/var/nuxt/src', '..', '/var/nuxt/src/pages')
+    expect(path.resolve).toBeCalledTimes(1)
+    expect(path.resolve).nthCalledWith(1, '/var/nuxt/src', '/var/nuxt/src/pages')
+    expect(path.join).toBeCalledTimes(1)
+    expect(path.join).nthCalledWith(1, '/var/nuxt/src', '..', '/var/nuxt/src/pages')
     expect(fsExtra.exists).toBeCalledTimes(2)
-    expect(fsExtra.exists).nthCalledWith(1, 'join(/var/nuxt/src, /var/nuxt/src/pages)')
+    expect(fsExtra.exists).nthCalledWith(1, 'resolve(/var/nuxt/src, /var/nuxt/src/pages)')
     expect(fsExtra.exists).nthCalledWith(2, 'join(/var/nuxt/src, .., /var/nuxt/src/pages)')
     expect(builder._defaultPage).toEqual(true)
     expect(consola.warn).toBeCalledTimes(1)
@@ -168,11 +171,10 @@ describe('builder: builder build', () => {
     )
 
     expect(builder._nuxtPages).toEqual(true)
-    expect(path.join).toBeCalledTimes(2)
-    expect(path.join).nthCalledWith(1, '/var/nuxt/src', '/var/nuxt/src/pages')
-    expect(path.join).nthCalledWith(2, '/var/nuxt/src', '..', '/var/nuxt/src/pages')
+    expect(path.resolve).nthCalledWith(1, '/var/nuxt/src', '/var/nuxt/src/pages')
+    expect(path.join).nthCalledWith(1, '/var/nuxt/src', '..', '/var/nuxt/src/pages')
     expect(fsExtra.exists).toBeCalledTimes(2)
-    expect(fsExtra.exists).nthCalledWith(1, 'join(/var/nuxt/src, /var/nuxt/src/pages)')
+    expect(fsExtra.exists).nthCalledWith(1, 'resolve(/var/nuxt/src, /var/nuxt/src/pages)')
     expect(fsExtra.exists).nthCalledWith(2, 'join(/var/nuxt/src, .., /var/nuxt/src/pages)')
     expect(builder._defaultPage).toBeUndefined()
   })
@@ -198,10 +200,10 @@ describe('builder: builder build', () => {
     await builder.validatePages()
 
     expect(builder._nuxtPages).toEqual(true)
-    expect(path.join).toBeCalledTimes(1)
-    expect(path.join).toBeCalledWith('/var/nuxt/src', '/var/nuxt/src/pages')
+    expect(path.resolve).toBeCalledTimes(1)
+    expect(path.resolve).toBeCalledWith('/var/nuxt/src', '/var/nuxt/src/pages')
     expect(fsExtra.exists).toBeCalledTimes(1)
-    expect(fsExtra.exists).toBeCalledWith('join(/var/nuxt/src, /var/nuxt/src/pages)')
+    expect(fsExtra.exists).toBeCalledWith('resolve(/var/nuxt/src, /var/nuxt/src/pages)')
     expect(builder._defaultPage).toBeUndefined()
   })
 })