Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Most of a function is being unexpectedly optimised out #16560

Closed
7 tasks done
bhallstein opened this issue Apr 29, 2024 · 4 comments
Closed
7 tasks done

Most of a function is being unexpectedly optimised out #16560

bhallstein opened this issue Apr 29, 2024 · 4 comments
Labels
bug: upstream Bug in a dependency of Vite p4-important Violate documented behavior or significantly improves performance (priority)

Comments

@bhallstein
Copy link

bhallstein commented Apr 29, 2024

Describe the bug

On vite 5 (currently testing on 5.2.10). We have this function in our codebase:

export default function post_routing(path, site, post_types) {
  if (!site) {
    console.log('//temp/')
    return RouteSite404
  }

  path = path.replace(/^\//, '')
  const path_components = get_path_components(path)
  const post_type_url = path_components[0]
  const post_url = path_components[1]
  const post_type = (post_types || []).find(item => item.url === post_type_url)

  if (path_components.length === 1) {
    if (post_type) {
      return {
        post_type_url,
        post_url: '__archive',
      }
    }
    else {
      return {
        post_type_url: 'pages',
        post_url: post_type_url,
      }
    }
  }

  else if (path_components.length === 2 && !post_type) {
    return RoutePage404
  }

  return {
    post_type_url,
    post_url,
  }
}

Which is being compiled to:

function EQ(e,t,n){return console.log("//site 404/"),Cw}

As you can see, everything after the first if statement is dropped. AFAICS this should not be happening for any reason I can find in the vite/rollup docs.

My suspicion was raised by reading about treeshake.correctVarValueBeforeDeclaration in the rollup docs, but there seems to be no reason this would affect this here, as site is an argument to the function that is passed in (and is always some object except in rare runtime scenarios). (And there is no other site variable in the relevant file.) However the issue still occurs when rollupOptions.treeshake.correctVarValueBeforeDeclaration is set to true.

The calls that invoke function elsewhere in the codebase look like this:

export async function GetCurrentRoute(path, site, post_types) {
  const route = post_routing(path, site, post_types)
  ...
}

and

const site = useSite()
...
useQuery({
  queryKey: KeyCurrentRoute,
  queryFn: () => GetCurrentRoute(...params.current),
  initialData: {...}
  ...
})

None of which seems to provide grounds for the compiler/optimiser to assume that site would be falsey in the if statement in the original function.

For comparison on vite 4.5.3, the function is compiled/minified, as expected, to:

function eX(e,t,n){if(!t)return console.log("//temp/"),Sw;e=e.replace(/^\//,"");const r=ZQ(e),o=r[0],i=r[1],a=(n||[]).find(s=>s.url===o);return r.length===1?a?{post_type_url:o,post_url:"__archive"}:{post_type_url:"pages",post_url:o}:r.length===2&&!a?f0:{post_type_url:o,post_url:i}}

I've tried to create a minimal repro of the issue but haven't been able to do so, when removed from the codebase vite 5.2.10 compiles this function just fine, not making the faulty code removal. Below is a link to my attempt to reproduce, but this works perfectly. (If anyone has any thoughts on how to go about reproducing this I would be very interested.) The linked repro repo now reproduces the issue.

Reproduction

https://github.com/bhallstein/vite-code-removal-repro

Steps to reproduce

  • npm i
  • npm run build
  • Examine dist/repro-[hash].js, note that the bulk of the post_routing function is incorrectly optimised out.

System Info

- Mac Mini M1
- tested with node versions 19, 20, 21, currently using 20.12.2
- tested with npm version 10.5.0

Used Package Manager

npm

Logs

No response

Validations

@bhallstein bhallstein changed the title Most of a function is being unexpectedly tree-shaken Most of a function is being unexpectedly optimised out Apr 29, 2024
@sapphi-red
Copy link
Member

I found a bug in rollup that might be related with it: rollup/rollup#5502
Does it work correctly if you override rollup version to 4.16.4? If you're using npm, add the following content to package.json and run npm i. If it works with it, I think it's caused by that bug.

{
  "overrides": {
    "rollup": "4.16.4"
  }
}

@sapphi-red
Copy link
Member

The bug has been fixed by rollup 4.17.2. Does it work with that?

@bhallstein
Copy link
Author

bhallstein commented Apr 30, 2024

@sapphi-red Confirmed, overriding rollup to 4.16.4 or 4.17.2 fixes the compilation/code-removal issue. The arg-spreading call triggering the issue in our codebase looks to be GetCurrentRoute(...params.current). Thank you so much for the help!

(This issue should maybe remain open until the fixed version of rollup is mainstreamed into vite?)

I have updated the linked repro repo which now does trigger the issue reliably.

@sapphi-red sapphi-red added bug: upstream Bug in a dependency of Vite p4-important Violate documented behavior or significantly improves performance (priority) labels Apr 30, 2024
@sapphi-red
Copy link
Member

Thanks for confirming!

I'll close this issue as Vite doesn't pin rollup minor/patch versions.

@github-actions github-actions bot locked and limited conversation to collaborators May 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug: upstream Bug in a dependency of Vite p4-important Violate documented behavior or significantly improves performance (priority)
Projects
None yet
Development

No branches or pull requests

2 participants